Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change returnKeyType while editing?

How can I change the return key on the iPhone while it is editing. I know about

myTextField.returnKeyType = UIReturnKeySearch;

However, this only works if I call [myTextField resignFirstResponder]; then [myTextField becomeFirstResponder]; which hides the keyboard then brings it back. Otherwise, it just stays as the one I had before, in this case it was "Go". I need to keep switching them depending on the characters the user enters.


In one line: How do I change the return key type of the keyboard while it is still editing?

like image 241
Ali Hamze Avatar asked Apr 17 '12 22:04

Ali Hamze


4 Answers

[textField reloadInputViews] seems to do the trick...

like image 115
Mark Beaton Avatar answered Nov 06 '22 18:11

Mark Beaton


I found it a while back, forgot to post back here. I am using:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: 
(NSRange)range replacementString:(NSString *)string {
    [self performSelector:@selector(addressBarTextDidChange:) withObject:nil afterDelay:0.1];
    return YES;
}

-(void)addressBarTextDidChange:(NSString *)text {
    NSString *addressText = @"..."; //Wherever you get your text from... 
    if ([addressText isEqualToString:@""]==NO) {
        if ([addressText rangeOfString:@" "].location != NSNotFound) {
            //[addressBarTextField setReturnKeyType:UIReturnKeySearch];
            if (addressBarTextField.returnKeyType!=UIReturnKeySearch) {
                [UIView beginAnimations:nil context:NULL]; //Setup the animation.
                [UIView setAnimationDuration:0.0]; //The duration for the animation.
                [addressBarTextField resignFirstResponder];
                addressBarTextField.returnKeyType = UIReturnKeySearch;
                [addressBarTextField becomeFirstResponder];
                [UIView commitAnimations];
            }
        } else {
            //[addressBarTextField setReturnKeyType:UIReturnKeyGo];
            if (addressBarTextField.returnKeyType!=UIReturnKeyGo) {
                [UIView beginAnimations:nil context:NULL]; //Setup the animation.
                [UIView setAnimationDuration:0.0]; //The duration for the animation.
                [addressBarTextField resignFirstResponder];
                addressBarTextField.returnKeyType = UIReturnKeyGo;
                [addressBarTextField becomeFirstResponder];
                [UIView commitAnimations];
            }
        }

    } else {
        if (addressBarTextField.returnKeyType!=UIReturnKeyDone) {
            [UIView beginAnimations:nil context:NULL]; //Setup the animation.
            [UIView setAnimationDuration:0.0]; //The duration for the animation.
            [addressBarTextField resignFirstResponder];
            addressBarTextField.returnKeyType = UIReturnKeyDone;
            [addressBarTextField becomeFirstResponder];
            [UIView commitAnimations];
        }
    }
}

Basically, I am animating the keyboard in and out for a duration of 0.0 seconds (so the user won't see it). I am also checking if the keyboard is not already the one I want before switching because constantly switching causes lag.

like image 29
Ali Hamze Avatar answered Nov 06 '22 18:11

Ali Hamze


In my case what it worked was the following:

sender.returnKeyType=UIReturnKeyNext;
[sender reloadInputViews];
[sender resignFirstResponder];
[sender becomeFirstResponder];

I had to force risignFirstResponder followed by a becomeFirstResponder.

like image 5
Javier Calatrava Llavería Avatar answered Nov 06 '22 17:11

Javier Calatrava Llavería


I made a custom Category to expose returnKeyType. Just import and do self.addField.returnKeyType = UIReturnKeySearch; or whatever you like to set as returnKeyType. You can also use it dynamically when the keyboard is already open. It will automatically refresh.

Here it is:

Interface:

/*

 Simple hack for exposing returnKeyType for UISearchBar without private APIs
 For you from CodeBuffet ;)


 Enjoy!

 ~PW

*/

#import <UIKit/UIKit.h>

@interface UISearchBar (ReturnType)

@property (nonatomic, readwrite) UIReturnKeyType returnKeyType;

@end

Implementation:

#import "UISearchBar+ReturnType.h"

@implementation UISearchBar (ReturnType)

UITextField *textField;

- (UITextField *) traverseForTextViewInViews: (NSArray*) views
{
    // Traverse over all views recursively until we find the tresure TextField hidden deep the UISearchBar ocean!
    for(UIView *subView in views) {
        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            return (UITextField *) subView;
        }
        UITextField *tv = [self traverseForTextViewInViews:subView.subviews];
        if (tv) {
            return tv;
        }
    }
    return nil;
}

#pragma mark Custom Setters & Getters

- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType
{
    if (!textField) {
        textField = [self traverseForTextViewInViews:self.subviews];
    }
    textField.returnKeyType = returnKeyType;
    [self reloadInputViews];
    [textField reloadInputViews];
}

- (UIReturnKeyType)returnKeyType
{
    if (!textField) {
        textField = [self traverseForTextViewInViews:self.subviews];
    }
    return textField.returnKeyType;
}

@end
like image 2
Peter Willemsen Avatar answered Nov 06 '22 17:11

Peter Willemsen