UITextView's becomeFirstResponsder not working proprly - iphone

I have a UIView with a UITextView and UITextField as a subview. I want the keyboard to appear automatically on textFieldShouldReturn call.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[textView becomeFirstResponder];
return YES;
}
but this does not work AND for some reason, it displays keyboard with cursor on next line in text view . I tried to set text nil in textview but it automatically call textDidChange method and cursor moves on new line without any text.
This same technique works correctly for a UITextField and also it works fine with iOS 5.0 but how to fix this in iOS 4.3. Let me know if you have any alternative as well.
Am I missing something obvious?

There's a known bug in iOS with resigning and becoming first responder within the same runloop. Try the following
[textField resignFirstResponder];
[textView performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

Related

UITextfield keyboard resign

in my uitableview in each cell I'm having uitextfield when user edits the textfield and after pressing any other button on the screen the Keyboard is not resigning. I did the following in textfield delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textfieldInCell = textField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
textfieldInCell=nil;// this is the ivar i am using for each textfield in cell.
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
I am also calling the shouldReturn delegate function once the user tapping on any other button but the keyboard is not resigning. Where am I going wrong?
Confirm that you bind the delegate of each textfield that you create with the view controller and add one line of code in textfield did end editing :-
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// add the following line
[textField resignFirstResponder];
textfieldInCell=textField;
// do the process...
textfieldInCell=nil
}
have you bind delegate of textfield with your controller? or did you check textFieldShouldReturn calling or not?
i think, binding is missing wit view controller in your case.
thanks
add a line in your tableview where you are adding textfield.
<YOUR_TEXTFIELD>.delegate = YES;
Enjoy Programming
first check that you give the delegate or not and give the delegate to UITextField after that when you call the method of button at that time resign this textfield like bellow...
-(IBAction)yourButton_Clicked(id)sender{
[yourTextField resignFirstResponder];
////your code write here
}
you must try to assign tag value to textfield then in should return method you used that tag to hide the keyboard like this (if you assign the tag -1 then)
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField.tag==-1){
[textField resignFirstResponder];
}
}

resign keyboard in UITextField

I'm working with storyboards on iOS 5 and have a simple screen that has a UITextField on it. I want to dismiss the keyboard when the user hits "Return". I followed other suggestions such as having my controller implements the UITextFieldDelegate protocol and implements textFieldShouldReturn: as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[questionField resignFirstResponder];
return YES;
}
However I see that this method is never called. I have set the controller of my view in storyboarding to my custom UIViewController.
I also tried a different implementation where I create an IBAction called dismissKeyboard but oddly I can't connect the Did Exit action to my UITextField.
Any ideas?
Update : So the problem seems to be that I'm using a UITextView and not a UITextField. I wanted a large area for the text to be entered. When I change the entry field to a UITextField it works fine. Any ideas on how to make it work with a UITextView?
You should connect the dismiss method to the text field's Did End Editing action.
So the solution to resign the keyboard for a UITextView is to implement shouldChangeTextInRange.
If you want the keyboard to dismiss when tap the return key for the textview use this delegate function,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

UITextField always resignFirstResponder

I am using a UIPickerView and I want my keyboard to never show up for some of my UITextFields. When I call resignFirstResponder on the UITextField after it is touched it doesn't make the keyboard go down.
- (IBAction) txtFieldClicked:(id)sender {
[txtField resignFirstResponder];
}
For each text field where you don't want to be able to show the keyboard, do:
[textField setUserInteractionEnabled:NO];
You can also check a corresponding block in the Interface Builder UI. This prevents the text field from receiving the touch event that would otherwise trigger it to display the keyboard.
Set text field delegate and implement its delegate methods
See UItextFieldDelegate
If you dont want the keyboard to pop up, return NO for all those text fields here
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqualTo:self.myTextField]) {
return NO;
}
return YES;
}

Resign keyboard when leaving textfield

Hey, I have created a textfield within two custom cells. One textfield shows the standard keyboard and the other the shows a pickerview when entered. The problem I have is when I move from the keyboard to pickerview textfield without clicking the "return" button on the keyboard, the keyboard doesn't resign. However when I do it using the "return" the keyboard resigns. Am using:
- (void)textFieldDidEndEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
}
and can't work out why this isn't working.
Thanks,
William
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == pickertextfield)
{
[textfield1 resignFirstResponder];
[pickertextfield resignFirstResponder];
}
return YES;
}
Does your view controller adopt <UITextFieldDelegate>? Is it hooked up as the delegate of your text fields?
Try using [self.view endEditing:YES]; before showing the pickerview and see it it helps.

iPhone: Possible to dismiss keyboard with UITextField clear button?

I'm wondering if there is a way to have the UITextField clear button 'always visible'
textfield.clearButtonMode = UITextFieldViewModeAlways;
doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?
Thanks in advance.
Like mentioned before it seems apple is setting the textfield focus after you clear the field.
The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
In your delegate, the function
- (BOOL)textFieldShouldClear:(UITextField *)textField
is called when the users wants to clear the textfield. If you return YES and call
[textField resignFirstResponder];
the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.
edit To make sure you really resign the responder, try doing it just a little later:
[textField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.1];
The delay didn't work well for me. Instead I added an instance variable to the delegate:
BOOL cancelEdit;
Then in the delegate implementation:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (cancelEdit) {
cancelEdit = NO;
return NO;
} else {
return YES;
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
cancelEdit = YES;
return YES;
}
UITextFieldDelegate textFieldShouldClear
- (BOOL)textFieldShouldClear:(UITextField *)textField {
[textField] resignFirstResponder];
return YES;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.
If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.