Resigning responder of textview - iphone

How to resign virtual keyboard for text view? is there any way same as textfieldshouldresignresponder?

[aVariablePointingToAnObject resignFirstResponder];
aVariablePointingToAnObject being your UITextView (or another object that becomes the first responder)
or... if that is too confusing for you then please refer to this:
[textView resignFirstResponder];

UITextView and UITextField are both subclasses of UIResponder and both support resignFirstResponder.

Related

after inputAccessoryView tapped

I have tried the solutions at UITextView doesn't show InputAccessoryView on first click
I'm having a with a problem with a textView who has a inputAccessoryView
I want to call [textView resignFirstResponder] through its inputAccessoryView,so that i can close the keyboard
and there is only a UIButton on the inputAccessoryView,and the tapping the button will invoke an IBAction method call -(IBAction)closeKeyboard:(id)sender;
now the sender in the method is the button on the inputAccessoryView,
question is ,
how can i find out this textView whose inputAccessoryView has been tapped,
or just get a pointed which is pointed to this textView,so i can call
[textView resignFirstResponder]??
You need to find the current first responder. If you have outlets to all your text views, you can ask each one in turn
if ([textView1 isFirstResponder])
[textView resignFirstResponder];
Though this may give you problems depending on your button (see here, but I haven't experienced this), if so use the editing property of the text view.

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];

UITableViewCell and resignFirstResponder

I have a UITableView containing in each cell a UITextField. When the user clicks on the UITextField, as expected, the keyboard will popup.
I have implemented in my delegate tableView: didSelectRowAtIndexPath: method to dismiss the keyboard with resignFirstResponder sent to the last UITextField used.
Everything works ok if the cell with the last UITextField used is displayed.
Now, if I scroll down to bottom of the tableview and press on a row, then the resignFirstResponder is sent to a hidden UITextField and will not hide the keyboard. It doesn't throw an error also.
How can I hide the keyboard in such cases?
Have a look at UITextFieldDelegate and put the
[textField resignFirstResponder];
method in one of the callback functions. I particulary prefer:
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Did you try calling endEditing:YES method of the UITextField?
-(IBAction)hidekey:(id) sender{
[textField resignFirstResponder];
}
if you using Interface builder then checked outlet of text field and in .m file use above function and define for that text field. It will work.
Please use this tutorial to Create a return Key for UIKeyboardTypeNumberPad ! This should save a lot of time for you.

how to check if a view is behind the keyboard on iphone

in my application i have a bunch of textlabels and textviews. Sometimes the textview is underneath the keyboard. My question is if there's a way to check if a textview is behind the keyboard to move it up. I already know how to move views up, and i know about the keyboardWillAppear notifications, but i don't know how to check if the view is behind the keyboard. The thing is that i don't want to move the textview if it's not underneath the kayboard. How can achieve that?
Thanks in advance.
I would do the check for first responder as shown above
[text isFirstResponder];
then I'd check to see if the bounds of the text field's bounds are less than 215 (because i think that's the max height of the keyboard) and accommodate from there. so all together it looks like:
if([text isFirstResponder]){
if(text.bounds.y > 215){
text.bounds.y = CGPointMake(text.bounds.y-(text.bounds.y-215));
}
}
I think the only way to see this is to verify each UITextField and UITextView if it returns YES for
[_text isFirstResponder];
If any UITextField or UITextView is First Responder, than it means that the keyboard is on the bottom of the screen.
You can see the keyboard will appear by listening to UITextFieldDelegate and UITextViewDelegate ShouldBeginEditing events:
for UITextField it is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;// return NO to disallow editing.
and for UITextView it is:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
Hope it helps.

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.