如何使用Next,Previous和Done Button键盘?

时间:2011-04-08 07:20:43

标签: iphone cocoa-touch ios4 iphone-keypad

我想要一个键盘,它上面有一个Next,Previous和Done按钮。

我在许多应用程序中都看到了这一点。

特别是有表格需要填写的地方。

enter image description here

我想实现与键盘上面类似的东西

我怎么能得到它?

8 个答案:

答案 0 :(得分:60)

你会在this other post找到答案。 我检查了iOS库,UITextField的inputAccessoryView正是您正在寻找的!

希望这有帮助!

答案 1 :(得分:25)

我刚刚创建了一个名为BSKeyboardControls的类,这使得将控件添加到键盘变得非常容易。可以找到类,说明和示例代码here at GitHub

这些控件适用于文本字段和文本视图,并针对iPhone和iPad进行了优化。

答案 2 :(得分:19)

-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField 

{
     UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    keyboardToolBar.barStyle = UIBarStyleDefault;
    [keyboardToolBar setItems: [NSArray arrayWithObjects:
                                [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(previousTextField)],

                                [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextTextField)],
                                [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)],
                                nil]];
    textField.inputAccessoryView = keyboardToolBar;

}



- (void)nextTextField {


    if (textField1) {

        [textField1 resignFirstResponder];
        [textField2 becomeFirstResponder];

    }

}

-(void)previousTextField
{

    if (textField2) {
        [textField2 resignFirstResponder];
        [textField1 becomeFirstResponder];
    }


}

-(void)resignKeyboard {

    [textField1 resignFirstResponder];
    [textField2 resignFirstResponder];

}

答案 3 :(得分:6)

我有一个实用程序类,基本上可以帮到你。

https://github.com/kalvish21/CustomKeyboard

这个想法非常简单。您必须添加一个带有条形按钮项的附件工具栏。有一个委托,它定义了该按钮的作用。

答案 4 :(得分:4)

https://github.com/asefnoor/IQKeyboardManager

这是迄今为止我见过的最好的键盘处理程序。管理文本输入的非常好的方法。

它的一些功能 1)零代码行

2)自动投放

3)没有更多的UIScrollView

4)没有更多的子类

5)没有更多的手工工作

6)没有#imports

答案 5 :(得分:1)

这是一个直接放在键盘上方的自定义控件。我认为可以使用UIToolbar。

上一个和下一个遍历textFields的firstResponder,Done将执行resign并隐藏工具栏。

要匹配键盘动画,请查看我发现的this code或在SO:"What is the iPhone's default keyboard animation rate?"

答案 6 :(得分:0)

我创建了一个repository,其中包含此功能的实现,可在所有方向的iPhone / iPad上运行,并且可高度自定义。

答案 7 :(得分:0)

正如其他答案所述,这是您正在寻找的inputAccessoryView

我想在这里添加另一种方法,使用这个cocoapods:

pod'UITextField-Navigation'

所有UITextField都有两个附加属性:nextTextFieldpreviousTextField。然后,您可以简单地在Interface Builder中连接nextTextField,并自动为您添加Previous,Next和Done按钮,所有功能,不再需要代码,也不需要子类化。

您还可以根据需要自定义UI,添加更多按钮等。

enter image description here enter image description here