使用苹果键盘中的返回按钮导航

时间:2014-09-02 07:06:18

标签: ios objective-c xcode xcode6

我的视图控制器中有多个文本字段如何使用IOS键盘中的返回按钮从一个文本字段导航到另一个文本字段。

5 个答案:

答案 0 :(得分:1)

使用UITextFieldDelegate委托和textFieldShouldReturn:方法。在里面,您可以获取传递参数的textfield标记,并根据此信息指向另一个标记。

要让代理工作,你必须将sender(textfield)的委托属性设置为接收者(例如你的视图控制器)

myTextField.delegate = self;

或在故事板中进行(here is some storyboard hints)。然后,您的视图控制器需要按如下方式指定此委托(在" h"文件中):

@interface MyViewController:UIViewController<UITextFieldDelegate>
@end

然后在&#34; m&#34;文件

-(BOOL)textFieldShouldReturn:(UITextField*)textfield{
    if([textfield tag] == 1)
    {
        //pass focus to next textfield
        [self.nextTextField becomeFirstResponder];
    } else {
        //remove focus from current textfield
        [textfield resignFirstResponder];//
    }
    return YES;//YES if textfield should implement its default behaviour
}

答案 1 :(得分:0)

-(BOOL) textFieldShouldReturn: (UITextField *) textField 
  {
   [textField resignFirstResponder];
    if(textField == _your1stTextField)
       [_your2ndTextField becomeFirstResponder];
    return YES;
  }

答案 2 :(得分:0)

试试这个......

将代理设置为所有文本字段,然后是..

   - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
       if (textField == textField1)
       {
         [textField2 becomeFirstResponder];
       }
       else if (textField == textField2)
       {
         [textField3 becomeFirstResponder];
       }
       else if textField == textField3)
       { 
         [textField4 becomeFirstResponder]; 
       }    
       .
       .
       .

       [textField resignFirstResponder];

       return YES;
    }

答案 3 :(得分:0)

我认为这个解决方案更具动态性,因为它适用于任意数量的textFields,只要你的superview没有带标签的视图== lastTextField.tag + 1

在viewDidLoad中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField1.delegate    = self;
    self.textField2.delegate    = self;
    ...
    self.lastTextField.delegate = self;

    self.textField1.tag    = 1;
    self.textField2.tag    = 2;
    ...
    self.lastTextField.tag = n;
}

然后实现textFieldDelegate:

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];

    if (nextResponder)
        [nextResponder becomeFirstResponder];
    else
        [textField resignFirstResponder];

    return NO;
}

答案 4 :(得分:0)

您需要在.h

声明

.m set

yourTextfieldRef1.delegate=self;
yourTextfieldRef2.delegate=self;
yourTextfieldRef3.delegate=self;
yourTextfieldRef4.delegate=self;
yourTextfieldRef5.delegate=self;
.
.
.

设置

yourTextFieldRef1.tag=1;
yourTextFieldRef2.tag=2;
yourTextFieldRef3.tag=3;
yourTextFieldRef4.tag=4;
yourTextFieldRef5.tag=5;
.
.
.


-(BOOL)textFieldShouldReturn:(UITextField*)textField;
    {
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (nextResponder) {
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
        } else {
            // Not found, so remove keyboard.
            [textField resignFirstResponder];
        }
        return YES;
    }

希望它可以帮助你......!