how to add done button in keypad

前端 未结 1 1673
你的背包
你的背包 2021-01-03 16:58

i need to add button done on keypad.

Apple does n\'t provide such felicity but some of application i found that done ,next,previous buttons.

like this.

相关标签:
1条回答
  • 2021-01-03 17:52

    1.Define the done button (= return key):

    textField.returnKeyType = UIReturnKeyDone;
    

    2.Add the action-listener:

    [textField addTarget:self action:@selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
    

    3.Define the action-event:

    - (IBAction)textFieldDoneEditing:(id)sender {
        [sender resignFirstResponder];
    }
    

    Have fun!

    EDIT:

    Here you can find detailed instructions how to add a Toolbar with Next & Previous above UITextField Keyboard: http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/

    EDIT2:

    Now, I have a really great example for you: "This view extends UITextView adding on top of the keyboard associated with this UITextView a toolbar with a « Done » Button"

    I check the code and it is a lot of easier than the first example:

    http://blog.demay-fr.net/2009/07/cocoa-how-to-add-a-toolbar-with-button-on-top-of-a-uitextview-in-order-to-add-a-dismiss-button/

    EDIT3:

    Hmmm, no, I doesn't test to code. But I will test it now!

    1.Problem: the right initialization. If I add the UITextView in IB, initWithCoder gets called:

    - (id)init {
    
        NSLog(@"init");
    
        if (self = [super init]) {
            //register a specific method on keyboard appearence
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        }
        return self;
    }
    
    - (id)initWithCoder:(NSCoder *)decoder {
    
        NSLog(@"initWithCoder");
    
        if (self = [super initWithCoder:decoder]) {
            //register a specific method on keyboard appearence
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        }
        return self;
    }
    
    - (id)initWithFrame:(CGRect)frame {
    
        NSLog(@"initWithFrame");
    
        if (self = [super initWithFrame:frame]) {
            //register a specific method on keyboard appearence
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        }
        return self;
    } 
    

    2.Problem: There's no view with the the Prefix "UIKeyboard":

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        NSLog(@"keyboardWindow = %@", keyboardWindow);
        for (UIView *keyboard in [keyboardWindow subviews]) {
            NSLog(@"keyboard = %@", keyboard);
    
                if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
                    // THERE'S NO VIEW 'UIKeyboard'!!!
                }
    
        }
    }
    

    The code doesn't work, I'm sorry... I don't know why there's no view "UIKeyboard"... Maybe the first example will help you at this point and you can build your own solution.

    0 讨论(0)
提交回复
热议问题