且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在键盘中添加完成按钮

更新时间:2023-02-08 20:54:27

1.定义完成按钮(=返回键):

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

textField.returnKeyType = UIReturnKeyDone;

2.添加动作监听器:

2.Add the action-listener:

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

3.定义动作事件:

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

玩得开心!

编辑:

您可以在此处找到有关如何添加工具栏的详细说明与Next&上面的UITextField键盘:
http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/

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:

现在,我有一个很好的例子:这个视图扩展了UITextView在键盘上的添加与此UITextView关联的工具栏带有完成»按钮

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-a-uxtxt-on-order-to-add-a-dismiss-button /

EDIT3:

嗯,不,我不测试代码。但我现在要测试它!

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

1.问题:正确的初始化。如果我在IB中添加UITextView,则会调用initWithCoder:

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.问题:前缀UIKeyboard没有视图:

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'!!!
            }

    }
}

代码不起作用,对不起......我不知道为什么没有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.