Hiding the UIKeyboard - objective-c

I am trying to hide the keyboard in my SplitView application (because it covers over part of the root menu). However, the only thing I can find is how to hide the keyboard after a textfield has been used [TextField resignFirstResponder].
Is there any other way to hide the keyboard?
Ideally I would like to use the barButtonItem, which displays the menu, as a trigger to hide the keyboard.

Use this:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

You need to send the -resignFirstResponder message to the instance of whatever UI element currently has first responder status. So if there were a firstNameTextField property on your class that corresponds to an instance of UITextField, you need to send the message to that object.
[self.firstNameTextField resignFirstResponder];

resignFirstResponder is way to do it. If you have a situation where your firstResponder is not set up as an instance variable (perhaps its generated), you can 'get' your firstResponder using this answer. After you have your first responder object, simply resign it!
hope this helps.

Related

NSView doesn’t catch keyDown events

I need to catch mouseDown and keyDown events in a subclass of NSViewController and as it’s not possible (please, correct me if I’m wrong) I left my GameViewController class blank and put all it’s methods to a new GameView class. In the identity inspector of GameViewController.xib I set my class to GameView while File’s Owner is set to GameViewController. After all, my mouseDown method works, but keyDown doesn’t.
I tried this:
- (BOOL) acceptsFirstResponder {
return YES;
}
but it didn’t help.
Please, show me what to do.
UPD: Actually keyDown method works, but first I need to click somewhere on the blank space of my window and only then everything works as it was planned. Once again, mouseDown events are triggered with no problems. What may I be doing wrong?
Your view accepts first responder when offered it, but nothing is attempting to make it the first responder until you click.
Set the window's initialFirstResponder some time before showing it. If you're using a NIB, it can be set there. Or you could do it programmatically.
After the window is shown, you can call [window makeFirstResponder:view] to make the view the first responder of its window.

UIPicker with UITextField

I made a research and all posts here are very blury regarding this issue.
I would like to use a UIPicker when pressing on a UITextField.
I would realy appriciate a step by step guide.
I tryd all posts here but every post gives me only a portion of what I need and I can't seem to connect it all together.
This is the last part of my application and i'm going crazy to finish it..
Thank you in advanced!
Gal
There is an inputAccessoryView property that contains a view that will appear instead of a keyboard on the bottom of the screen. Create a UIPicker, adjust its frame, provide values and assign it to the inputAccessoryView property.
UIPicker will appear when user taps on your UITextField.
If you don't need editing, you may use a UILabel instead of the UITextField. Solution is the same. I have a ready-made class if you need.
Here's a way:
http://www.youtube.com/watch?v=t_F1ex5opgA&t=14m10s
-(BOOL)textfieldShouldBeginEditing:(UITextField *)textField
where textField is the name of your text field.
Call your UIPickerView and return NO so that your picker is loaded rather than the keyboard.
The idea is to call an action that opens the UIPicker when the user taps the UITextField. Because the UITextField does not responde to the usual touchUpInside events that UIButtons respond to, I would just overlay a transparent UIButton on top of the UITextField and just in case, make the text field's userInteractionEnabled property NO. Hook the UIButton to responde to touchUpInside and call a method that opens the UIPicker. Another option would be an immediate response to the text field's touch by implementing "textFieldShouldBeginEditing" and immediately resigning the text field.
The next step would be to present the UIPicker - if we are talking about iPad, this would best be done by using a UIPopoverController. On iPhone, maybe consider bringing it up modally. When you create the view controller that holds this UIPicker, be sure to add a delegate property to it so that whatever value that was selected on the picker can be transfered back to the main view controller and on to the UITextField.
Hope this helps with getting you started.

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.

UITextfield in UITableViewCell resign FirstResnponder

I have a UITextfield of type NumberPad in a UITableViewCell. How can I make it resign first responder (i.e. dismiss the keyboard)?
Thanks
You have to add a UIButton somewhere in your interface. I often do it on the navigation bar and then resign first responder when it is pressed.
There are all sorts of kludges to superimpose fake done buttons over the numberpad, but they are asking for trouble IMHO. It would be nice if Apple addressed this in future though as lots of people have the same issue.
Good discussion and other solutions here;
How to show "Done" button on iPhone number pad
Implement UITextFieldDelegate and set your UITextField delegate to it. In the -(BOOL)textFieldShouldReturn:(UITextField *)textField delegate method you set the resignFirstResponder call.
You can also set up an action to a button on the pad. You can do this by trying to add a done button to the pad. I know that in the most recent sdk this is somewhat impossible.
For those cases you have two options:
1.Add a done button somewhere on the view. If you are using a navigation controller you can add it to the navigation bar and simply set up the action for that button and in the action you resignFirstResponder for the UITextField.
2.Add your keyboard to a UIActionSheet and add the done button right on top of the keyboard.

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