NSTextField is not working over Full Screen - objective-c

NSTextfield is not working when I make a full screen app.
I have also made first responder of textfield. How can I make it enable and working?

Full Screen window won't get any keyboard events the way it is, due to the fact that it was created with with the NSBorderlessWindowMask style mask. Apparently windows of this type can't become "key windows". However, you can fix this problem by subclassing NSWindow and overriding the canBecomeKeyWindow method.
- (BOOL)canBecomeKeyWindow
{
return YES;
}

Related

NSTextField in status bar doesn't want to receive focus

For some reson sometimes a NSTextField I'm using in Status Bar menu doesn't always allow me to input text. I click it and nothing happens as if it was disabled. Upon restarting program it works again. I don't do anything with it, it's just created in the interface builder.
That's because no NSWindow contains the NSTextField. The NSWindow sets the first responder when the window gets the main window. The NSStatusBar is global. It's never focused so your textfield only will be focused in the very beginning.
I'm not sure if there's a way to solve this problem in a nice way. You might try to set the first responder manually. You could also add a global event monitor
Example:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent* incoming) {
[textfield setStringValue:[incoming characters]];
}];
Note: This is a very bad way to fix this problem. I'd first try to set the NSTextField manually as a first responder if this is possible.

Problem avoiding first responder on a NSTextField

I need to change behavior of input fields in a really simple app:
Whenever i launch the application the first text field get the focus, but i don't want this behavior.
I tried checking "Refuses first responder" in IB. It works but with this option checked i can't move between input fields pressing "tab" button.
What can i do to avoid focus at startup and keep the ability to move with tab keyboard button ?
The (previously) accepted answer isn't reliable and doesn't work very well. The other answer with the hidden NSTextField isn't very great either because now you have a new element in your Tab order.
The solution I've found works best so far is:
Make the NSTextField refusesFirstResponder YES on app launch.
Then, in viewDidAppear for the controller, go ahead and set refusesFirstResponder back to NO.
Everything behaves perfect after launch, and I don't have a greedy NSTextField stealing first responder on app startup.
I found the solution, you can add [window makeFirstResponder:nil]; after awakeFromNib for example in applicationDidfinishLaunching.
window?.makeFirstResponder(nil) does not work for me - when I check who is the first responder, it is the window (and not a NSTextField) but still, the first NSTextField is selected and active. The solution for me (though I know not the cleanest one) was to create a hidden text field and make it the first responder every time the window did load.
window?.makeFirstResponder(nil) worked only when I set all NSTextFields to RefuseFirstResponder, but then using Tab to switch between them of course do not work.
This worked for me,
override func viewDidAppear() {
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { [weak self] (timer) in
NSApplication.shared.windows[0].makeFirstResponder(self?.textUsername)
let tRange = self?.textUsername.currentEditor()?.selectedRange
self?.textUsername.currentEditor()?.selectedRange = NSMakeRange((tRange?.length)!, 0)
}
}

Cocoa: NSTextField and Fullscreen-Mode with enterFullScreenMode:

currently I'm experimenting with the enterFullScreenMode: option of NSWindow making a subview of the window's contentView the new fullscreen view which works nice. However, my view contains a NSTextField which behaves weird. When I switch to fullscreen mode it becomes inactive (seems to resign firstResponder status). I can use it just fine by clicking it and by calling
[myField becomeFirstResponder];
which is discouraged by the docs. They say I should always call
[myWindow makeFirstResponder: myField];
which does not work any more after being in fullscreen mode. The weirdest thing however is that when entering something in the field and then exiting the entered text disappears. When switching back fast enough to fullscreen mode it might even be back again. Any idea what I'm doing wrong? Or any feedback on how to make the NSTextField resign firstResponder status in fullscreen mode without being using discouraged API calls?
Thanks in advance,
Nicolas
You mean in 10.6 right?
[myWindow makeFirstResponder: myField];
enterFullScreenMode will make a new window for the view, so myWindow actually is the window before entering fullscreen.
you should use
[[self window] makeFirstResponder: myField];

IPad dismiss Keyboard without knowing which Textfield opened it

Is there a way to do a general resignFirstResponder to hide the keyboard regardless of what textfield/view/etc calls it?
Reason is I have a lot of textfields on my view and don't want to have to resignFirstResponder for all textfields to hide the keyboard. Just want a general
[self resignFirstResponder].
Any ideas?
Thanks in advance
I know that this has already been marked as answered, but for those that run into this like I did you can just use the following method on the view that contains the textfields.
- (BOOL)endEditing:(BOOL)force
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign. UIView Documentation
[self.view endEditing:YES];
it will hide keyboard when we click on view.
You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
this will dismiss the keyboard globally without the reference.
hope this will help you.
The easiest way to do this is to have a method for whenever you want to dismiss the keyboard that looks like this:
-(void)dismissKeyboard {
[firstField becomeFirstResponder];
[firstField resignFirstResponder];
}
You can check these questions:
Is it possible to make the iPhone keyboard invisible / remove it without resigning first responder?
Hide Input Keyboard on iPhone Without Knowing First Responder?
In summary:
You can call becomeFirstResponder on some other thing that you choose. It could be a UIViewController or a UIView. I had a similar problem before, I needed to make my keyboard go away when I was pushing my view controller back to its caller, without knowing which textfield was the first responder. Then, on viewWillAppear of my view controller which I was returning back, I called [self becomeFirstResponder] and the keyboard of the pushed view was gone. Because this made whichever text field was it loose being the first responder
In my own app when I had more than one text field and would like to make the keyboard go away regardless which of the fields called it, I would just wrote a method and let each and every of them resignFirstResponder.
I assume that as a programmer, you should have the clear knowledge how many text fields are on your view controller and how you can access them, otherwise it'll get messed up and you app won't look good... :-P

Changing the zoom out action for a UIWebView

I'm trying to change the way a UIWebView reacts when the user do a pinch out gesture (when he's doing that, I zoom out and show multiple webView).
I see 2 solutions but I can't make them work :
Setting scalesPageToFit to YES, and
then getting events that are sent by
the API to change the behavior.
Problem: I don't find how to catch those events
Setting
scalesPageToFit to NO, and adding a
PinchGestureRecognizer on the parent
of the UIWebView. Problem:
The gesture is not well recognize
since there are conflicts with the
scrolling. If the user starts moving
the first finger before putting down
the second finger, the scroll begin
and the pinchGesture is not
recognize.
Do you have an idea ?
Ok, the anwser is just to set the delegate of my UIPinchGestureRecognizer to self and to override
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherRecognizer
{
return YES;
}
Now the pinch is recognized even if the scroll began in the UIWebView