Adding and removing keyboard - iphone

I have a textfield and when the user clicks on it, they will be presented with the keyboard. there is a GO button on the keyboard, and i want to write an action event to it.
1.) How can i write an action when the user clicks on this button
2.) when the keyboard is open, and when the user clicks on the background i need the keyboard to disappear, how can i do this programatically?
I have no code to demonstrate, i have only added a texfield, so the keyboard would appear by default once clicked

for performing some action on return of textfield/ Go button use following code
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
//call Method when the GO button is pressed
return YES;
}
And when user touches on the background and keyboard should return - for that ,write below code
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textFiedl resignFirstResponder];
}
hope your issues are resolved with this.

A textfield is present whenever a UITextField or UITextView is the first responder. You can manually "show" the keyboard by calling becomeFirstResponder or "hide" it by resignFirstResponder.
In your case, Look at the UITextFieldDelegate reference; when the user hits "GO", the textFieldDidEndEditing: callback is invoked. In this method, you should call resignFirstResponder on the text field to hide the keyboard.

For hiding the keyboard when you touch on the background you can write [txtName resignFirstResponder]; where txtName is the reference name of the TextField.

Related

delegate called on leaving the foucus on textbox

textFieldShouldEndEditing is calling only when leaving the focus from first text field and focus the second text field.. here i want to call the delegate when i click outside the text box rather than focus the other text box.
daclare an instance to UITextField: UITextField *currentTextField;
in textfieldDidBeginEditting say currentTextField = textField;
place a custom button without image and title all over the screen and give it the action below:
Now your delegate will get called when you tap wherever on the screen (excluding other controls).
-(IBAction)hideCurrentKeyboard{
[currentTextField resignFirstResponder];
}

How To Resign The UITextView With Done Button

i was wondering if any of you knew how to get access to that done button that appears above the keyboard when editing.
I have seen it before, above the keyboard there is a transparent black area and on the right there is a blue "Done" Button.
I could do this by hand with my own animations and buttons above the keyboard in my app to resign the UITextView, but i would prefer to use Apple GUI elements that people know.
So does anybody have any information about where this "Done" button comes from?
You don't really "get access" to that button, but through the UITextViewDelegate protocol, you essentially can:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html
Implement the
- (void)textViewDidEndEditing:(UITextView *)textView
routine, and assign the UIViewController holding the textview to the textview' delegate.
Inside of that routine, you can do what you wish! You will need to call the
resignFirstResponder
method.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/resignFirstResponder
You will have to implement the style of keyboard that implements that blue done button.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/occ/intf/UITextInputTraits
FINAL ANSWER
- (void)textViewDidEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
}

show subview when UITextField touched

I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];

How to hide the iPhone keyboard? resignFirstResponder does not work

I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.

UIButton not disabled when UITextField has focus

I have a UIButton - a submit button - that I set the enabled and disabled states and titles for. I use the submit button title to show status like #"Sending..." while my program is making an api call by disabling the button.
It works fine until the edge case where someone enters a comment on a UITextField, but instead of dismissing the keyboard/finishing editing just hits the SUbmit button (so the keyboard is still up).
In this case, disabling the button doesn't change the title and background to the disabled state.
Is this an issue with firstResponder? I'm trying to explicity tell the comment field to resignFirstResponder before setting the button.enabled = NO, but it still doesn't update the button.
Are you implementing this UITextFieldDelegate method:
- (void)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
button.enabled = NO;
}
and then resigning the text field's first responder and disabling the button?
Sometimes textFieldSHouldEndEditing doesn't get called since the field may not lose focus, so I do try to explicity tell the text field to resign before disabling the button. Doesn't seem to work...