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

时间:2011-02-03 07:34:03

标签: iphone

我需要在键盘上添加按钮。

Apple并没有提供这样的幸福,但是我发现了一些应用程序,它们已经完成了,下一个,以前的按钮。

like this

我如何添加这些以及如何向他们提供点击事件。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

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

textField.returnKeyType = UIReturnKeyDone;

2.添加动作监听器:

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

3.定义动作事件:

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

玩得开心!

修改

在这里,您可以找到如何使用Next& amp;添加工具栏的详细说明。上面的UITextField键盘: http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/

<强> EDIT2:

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

我检查代码,它比第一个例子容易得多:

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:

嗯,不,我没有测试代码。但我现在将测试它!

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

- (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”没有视图:

for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
    NSLog(@"keyboardWindow = %@", keyboardWindow);
    for (UIView *keyboard in [keyboardWindow subviews]) {
        NSLog(@"keyboard = %@", keyboard);

            if([[keyboard description] hasPrefix:@"&lt;UIKeyboard"] == YES) {
                // THERE'S NO VIEW 'UIKeyboard'!!!
            }

    }
}

代码不起作用,对不起......我不知道为什么没有视图“UIKeyboard”......也许第一个例子会帮助你,你可以建立自己的解决方案。