NSAlert keyboard event propagation - macos-high-sierra

I wrote an app using Xcode 9.4 and Swift 4.1 for Mac OS X High Sierra. It has a custom NSView and a custom NSViewController. The custom NSView class has override functions defined for handling keyboard events (keyUp and keyDown). (It also has an override for acceptsFirstResponder property that returns true.)
If the user types X key, the app builds an NSAlert object with message text and 2 buttons and displays it in sheet modal form. The alert can be dismissed in one of 3 ways:
mouse clicked on one of the NSAlert buttons
using SPACE bar from keyboard
using RETURN key from keyboard
The problem is that in cases 2 and 3 the keyboard event for the key used to dismiss the NSAlert is propagated to the custom NSView keyboard handlers. Is there a way to disable this propagation?

Related

Disable NSWindow Interaction

I am attempting to develop a custom NSAlert using NSWindow. When the custom alert NSWindow is triggered from the main application window, the user is still able to interact with the main window and access the menu bar whilst the alert window is still active. I believe when a NSAlert is presented, such actions are disabled and a 'Funk' sound is played upon any attempt to interact with the main window.
How would I disable interaction with the main window (and possibly play a 'Funk' sound) until the user has acted on the alert window?
Run your custom window as a "modal" window. The simplest way is to use the runModal(for:) method of NSApplication. Actions which should complete or dismiss the modal dialog should call stopModal() or stopModal(withCode:).

OS X Menubar application: How to bring window to front

I am developing a menubar-only application for OS X and I am struggeling to get a settings window to show up in front of other apps.
App setup
"Menubar-only application" means:
I removed the "Is Initial Controller" from the NSWindowController in the main storyboard file. The main storyboard's window is not used in my app
I added an NSMenu to the "Application Scene" in the main storyboard. This will become my menubar menu
I set LSUIElement to YES to hide the dock icon
I set LSBackgroundOnly to NO (see NSWindow makeKeyAndOrderFront makes window appear, but not Key or Front)
When the app starts, I create an NSStatusItem and add the NSMenu from the storyboard as its menu. This all works fine - the app starts, shows no window and no dock icon but a menubar item that contains the menu from the storyboard.
Settings window
I now wanted to add a settings window that is shown when a menubar entry is clicked. I therefore:
Created a new .xib-file and added an NSWindow to it
Created a custom NSWindowController that connects the outlets and actions
Instantiated the custom NSWindowController using initWithNibNamed: on app launch
When the "Settings"-entry from the menu is clicked, I then try to bring the settings window to front using:
[self.settingsWindowController.window center];
[self.settingsWindowController.window showWindow:self];
[self.settingsWindowController.window makeKeyAndOrderFront:NSApp];
The window is shown, but not brought to the front but rather hidden behind other apps.
Any ideas how to solve this? Thanks!
You need to call:
[NSApp activateIgnoringOtherApps:YES];
(This is one of the rare occasions where it's correct to pass YES to that method.)
For Swift you can use
NSApp.activate(ignoringOtherApps: true)
I know you're asking for obj-c and already received an answer, but just incase anyone is Googling for Swift 4 implementation.
In your class that extends NSWindowController define the function:
func bringToFront() {
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
Then whenever you wanna bring it up, call bringToFront().

No UIControls responding to user events

I am creating an iOS app without using Interface Builder and I seem to be missing something vital whereby the controls I am creating (UITextField, UIButton, etc.) are not responding to touch events.
Here's my view hierarchy:
UIWindow->UIView->(UITextField, UIButton)
I am able to create the above hierarchy and everything is showing up fine on the screen, but tapping on the UITextField or the UIButton do nothing. I also tried adding some UIGestureRecognizer subclass instances to the UIView to no avail.
I am able to call becomeFirstResponder on the UITextField and the application starts with the keyboard up and able to receive input. However when the keyboard is dismissed the interface goes back to its "dead" mode.
What am I missing?
I was calling init on my UIWindow instead of initWithFrame:. For some reason everything was drawing correctly but the UIWindow was not responding to events because every user tap was outside the bounds of the zero-size window.

How do I enable the done key and the return key in ipad objective c

Is it possible to enable or show both the "return" and "done" button on the ipad keyboard? If so, How or how to work around?
I have a UITextField that is multi-line and want to add a done button to the key board.
I dont think the keyboard can display both without augmenting the layout manually - Additionally this is not app store safe.
I have done this before by hooking the
-(void)textFieldDidBeginEditing:(UITextField *)_tf
In the UITextFieldDelegate. Although there might be a better keyboard based event to use. If your interested i will post the code.
On this event, A small toolbar appeared flush with the top of the keyboard containing next, previous and Done.
I delegated the button events from the toolbar back to the ViewController that was responsible for the TextField

Whats the best way to have a UIView for a popup Keyboard in a UIViewController?

I'm currently developing an iPhone application and part of it has a custom keyboard which pops up, this currently consists of
UIViewController
(1) UIButton (to activate keyboard)
UIView
-- 10 x Input buttons (part of the keyboard)
When the app starts I set the UIView to be out of view, and when the button (1) is clicked the UIView animates up from the bottom of the screen, this works perfectly but it got me thinking "Is this really the best way to do this?".
It would be nice if I could have my custom keyboard separate to the UIViewController so that in future apps I could just include the files which build up the UIView / Keyboard.
Any advice would be much appreciated.
Is inputAccessoryView what you're looking for?
The custom accessory view to display
when the text field becomes the first
responder
The default value of this property is
nil. Assigning a view to this property
causes that view to be displayed above
the standard system keyboard (or above
the custom input view if one is
provided) when the text field becomes
the first responder. For example, you
could use this property to attach a
custom toolbar to the keyboard.