Why is the NSStatusItem displaying multiple times? - macos

A NSStatusItem has a NSMenu attached, and one of the buttons of the NSMenu opens a NSWindow. Whenever one of these buttons is clicked, the window opens as expected and works properly, but another display of the NSStatusItem is opened.
The NSStatusItem is a clock, so I can see that it is updating correctly. However, the cloned NSStatusItem doesn't have its own menu. If I push the button that makes the window more times, more cloned versions of the NSStatusItem pop up.
Everything works fine except for this.
That's not a whole lot of information to go off of, but there's nothing else I can think of that could potentially help you. I would be happy to provide more information or try something.
EDIT: Every time the button is clicked, awakeFromNib is somehow called, which is why another half-working NSStatusItem happens.
EDIT: Temporary workaround is to put the awakeFromNib method in a dispatch_once.
EDIT: Added method that is triggered when button is clicked, as suggested by #zpasternack
- (IBAction)preferences:(id)sender {
self.windowController = [[NSWindowController alloc] initWithWindowNibName:#"PreferencesWindow"];
[[self windowController] showWindow:self];
}

Is the NSStatusItem contained in the PreferencesWindow nib? That might explain it, since you're loading the nib each time the button is clicked.
Also, is there a reason you need to recreate that window each time the button is clicked? Maybe you could only do it the first time?
- (IBAction)preferences:(id)sender {
if( self.windowController == nil ) {
self.windowController = [[NSWindowController alloc] initWithWindowNibName:#"PreferencesWindow"];
}
[[self windowController] showWindow:self];
}

Related

NSButton Doesn't Respond to Click When Putting inside M3NavigationView

I'm using M3NavigationView to do view navigations, but there I got a problem when push one view inside(some animation happens here), A button doesn't responds to mouse click most of the time, I'll have to click it twice,
I clicked once on the button, and it seems the mouse up event doesn't happen. I have to click the button again to trigger my action.
After more testing today, I found the button inside works normally after it is displayed for 2 seconds. more weird.
Is there anyone knows that going on here?
I used one out of two of my apple developer support service, and get it solved. row double clicked of PXListView needs to change a little to work with M3NavigationView
if([theEvent clickCount]>1) {
if([[self delegate] respondsToSelector:#selector(listView:rowDoubleClicked:)]) {
[[self delegate] listView:self rowDoubleClicked:[theCell row]];
return; //return here, this is the change
}
}

Navigation Bar Layout Issue on Tab Bar Controller + Navigation Controller

I've followed a how-to to create a simple Tab bar controller with a navigation controller in the first tab. Until here all is working correctly, expect a strange issue on the layout.
When the app starts the first time, the Navigation Bar on the top of the first loaded nib is a little outside of the view. I cannot figure out why this happen. In the first view there is a button "Add new System" that opens a modal view. If I press this button and the modal view appears and then I dismiss the modal going back to the initial view, then the Navigation bar at the top is placed/refreshed correctly. The same happens if I press the second TAB (it's a simple nib without Navigation controller for now) and then back to the first TAB, the Navigation bar is placed in the correct position.
Here a screenshot on the first startup:
And here when I press the modal view or the second TAB and then back to the first view:
The code is quit simple following one of the numerous tutorials on the net. I'm NOT using storyboard. Only customization was adding the buttons on the top of the Navigation Bar:
UIImage *editbuttonImage = [UIImage imageNamed:#"edit_pressed.png"];
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[editButton setBackgroundImage:editbuttonImage forState:UIControlStateNormal];
editButton.frame = CGRectMake(0, 0, editbuttonImage.size.width, editbuttonImage.size.height);
[editButton addTarget:self action:#selector(leaveEditMode)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:editButton];
[editButton release];
[editbuttonImage release];
No other modifications were made. The nib was used before in a single view. Then I've tried to insert it into a TAB Controller + Navigation Controller.
I could post the whole code in case it's needed. Under Select System there is a Table View, in these pictures empty, also not shown.
Thank's for the help!
Simon
I've solved the issue myself. On startup I've setup to hide the status bar and shown it again in the app delegate. The directive used :
[[UIApplication sharedApplication] setStatusBarHidden:NO];
was after adding the navController as subview. Also the Navigation controller BAR was not out of the view, simply under the status bar.
Hope this helps someone :)
Cheers, Simon

How to change the NSScreen a NSWindow appears on

I have an application that will load a couple of windows depending on which button is pressed. All except one of these open on the mainScreen (the screen in which the main window is open in). One of them (the preference window) opens on the first screen (the screen with the menu bar). I cannot understand way it is doing this, is there a way to change the screen that a NSWindow opens on?
I could not get toohtik's answer to work. What I ended up doing was subclassing NSWindow and then overriding constrainFrameRect: toScreen:. This will automatically open the new window on the "main screen" of the application.
- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
{
AppDelegate *delegate = [[NSApplication sharedApplication] delegate];
return [super constrainFrameRect:frameRect toScreen:delegate.window.screen];
}
I dont't know why you have that behaviour but you can change it through initWithFrame method that takes NSScreen argument.

resignFirstResponder not hiding keyboard on textFieldShouldReturn

I have a view with a UITextField which should hide the keyboard when return is pressed.
My function is this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( textField == userPassword ) {
[textField resignFirstResponder];
}
return YES;
}
Normally the keyboard should be hidden but it stays on the screen. resignFirstResponder is correctly called. What am I missing?
I see you have the iPad tag on this. Do you happen to be presenting a modal view using UIModalPresentationFormSheet? If so, it looks like this is a limitation of the FormSheet modal presentation (either Apple is doing it intentionally for some reason, or it is a bug). See these other questions for more details:
Modal Dialog Does Not Dismiss Keyboard
Modal View Controller with keyboard on landscape iPad changes location when dismissed
There is this helpful method which allows you to dismiss the keyboard when presenting the Modal Dialog:
- (BOOL)disablesAutomaticKeyboardDismissal { return NO; }
This will override the default behavior of the modal dialog set by Apple and allow you dismiss the keyboard. It is in the UIViewController Class.
I hope this helps someone!
If you are using the Interface Builder, look if your UITextField has the delegated linked with your class.
-Select your UITextField and in your Connections look if exits one connection in Outlets->delegate. If not, conect with you File's Owner Class.
This need to be linked with your File's Owner Class. This delegate tell where to search for a method. If your are overriding a method, you need to tell where the object will search for that.
This solution worked for me after none of the above did. after calling resignFirstResponder i added a modal view & removed it.
[myTextField resignFirstResponder];
UIViewController *dummyController = [[UIViewController alloc] init];
UIView *dummy = [[UIView alloc] initWithFrame:CGRectMake(-1, -1,1,1)];
[dummyController setView:dummy];
[self presentModalViewController:dummyController animated:NO];
[dummyController dismissModalViewControllerAnimated:NO];
To deal with the bug mentioned by Brandon, you can try closing and re-opening your modal view controller as long as you still have a reference to it.
[textField resignFirstResponder];
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:yourModalViewControllerReference animated:NO];
(where "self" should be the controller you used to originally open the modal view controller)
I was having the same problem. I realized that after connecting the delegate to the File's Owner in Interface Builder, I neglected to save in Interface Builder. Once I saved, I recompiled and the keyboard disappears correctly when hitting return.
xcode 4.5.1
Simply click control then on the textfield drag and release on the .h file
(control key+ drag)
then in the pop up menu select
connection=acton;
name= any name;
type=id;
event=did end on exit;
arguments=sender;
then click connect button
Did you remember to implement the UITextFieldDelegate protocol?
I have read so many articles about this issue, where the onscreen keyboard refuses to hide when you call resignFirstResponder, but none of the suggestions worked for me.
I'm using XCode 5 (iOS 7) and have a iPhone screen containing a couple of controls which require the onscreen keyboard, but if the user clicks on the UIButton, then I want the keyboard to disappear.
I probably wasted one full day experimenting with resignFirstResponder and adding disablesAutomaticKeyboardDismissal functions to return NO, but nothing worked. Once the onscreen keyboard appeared, I could never get it to disappear again.
But then I had a small brainwave (as I only have a small brain).
Now, when the user clicks on my UIButton, I simply disable the UITextField and UITextView controls.
- (IBAction)btnDate_Tapped:(id)sender {
// The user has clicked on the "Date" button.
self.tbClientName.enabled = NO;
self.tbComments.editable = NO;
And suddenly, the app finds it has no editable text fields needing an onscreen keyboard, and it neatly slides the keyboard out of sight.
(Relieved sigh.)
My UIButton actually makes a popup dialog appear. When the user dismisses the popup, I re-enable these two controls, so if the user taps in one of them, the keyboard will appear again.
-(void)popoverControllerDidDismissPopover:(UIPopoverController *) popoverController {
// The user has closed our popup dialog.
// We need to make our UITextField and UITextView editable again.
self.tbClientName.enabled = YES;
self.tbComments.editable = YES;
... etc...
}
Simple, isn't it !
And surprisingly, this workaround even works on UIViewControllers which appear in Modal style.
I hope this helps other XCode victims out there.
Based on your comment that it looks like focus has shifted, then I think what may be happening is that the keyboard is staying open for the next text input field. If your return key is a "Next" key, then returning YES for textFieldShouldReturn: will make the next textField the first responder, and keep the keyboard visible.
The easiest way is:
Go to your user interface builder,
select UITextField and "Control-Drag" to "Detail View Controller-Detail" and release.
The window will pop-up. Then under "Outlets" select "Delegate".
That's it. It worked for me.
if you are in UIModalPresentationFormSheet just call
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
Swift 3.0:
override var disablesAutomaticKeyboardDismissal: Bool {
get{
return false
}
set {
self.disablesAutomaticKeyboardDismissal = false
}
}
Swift 3.0
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == addressTextField {
textField.resignFirstResponder()
return false
}
return true
}

LSUIElement behaves inconsistently with activateIgnoringOtherApps

Specifically, it behaves inconsistently regarding text field focus.
I have an LSUIElement popping up a status menu. Within that menu there is a view containing a text field. The text field needs to be selectable -- not necessarily selected by default, but whichever.
When the status item is clicked, it triggers
[NSApp activateIgnoringOtherApps:YES];
And it works, about half the time.* The other half the status menu seems to consider itself "in the background" and won't let me put focus on the text field even by clicking on it. (I know the status item click-trigger is firing b/c there's an NSLog on it.)
Is this a bug in the way Apple handles these status items, or am I mishandling activateIgnoringOtherApps?
*In fact, it seems to fail only the first time after another app is activated. After that it works fine.
The complete snippet:
-(void)statusItemClicked:(id)sender {
//show the popup menu associated with the status item.
[statusItem popUpStatusItemMenu:statusMenu];
//activate *after* showing the popup menu to obtain focus for the text field.
[NSApp activateIgnoringOtherApps:YES];
}
Finally came up with a workaround for this.
Instead of popping the menu in your click handler, activate the app then schedule an NSTimer with no delay that pops the menu:
-(void)pop:(NSTimer *)timer {
[statusItem popUpStatusItemMenu:theMenu];
}
-(void)statusItemClicked:sender {
[NSApp activateIgnoringOtherApps:YES];
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:#selector(pop:) userInfo:nil repeats:NO];
}
pop: is called on the next frame so the delay is imperceptible but long enough for activateIgnoringOtherApps: to do whatever was preventing it from working as expected when popping the menu in the same frame.
I know from experience that you have to call activateIgnoringOtherApps: after you've popped up the menu that contains your text field. So you would need to do it in this order:
- (void)statusItemClicked:sender {
[statusItem popUpStatusItemMenu:theMenu];
[NSApp activateIgnoringOtherApps:YES]; // FYI, NSApp is shorthand for [NSApplication sharedApplication]
}
Based on what you've said, it sounds like your application is activating too late, so that it's not getting activated the first time you click on the item, but it is already activated on subsequent clicks.

Resources