如何使用“下一个上一个”按钮转到下一个字段

时间:2013-02-16 00:52:57

标签: ios ios5 uiviewcontroller uitextfield

我已经设置了一个包含两个字段的视图控制器。我在键盘上方实现了一个工具栏,其中包含Previous和Next的按钮。

我有一个字段列表数组。

    // Setup array of listed fields
textFields = [[NSArray alloc] initWithObjects:usernameField,passwordField, nil];

工具栏

(id)initWithTextField:(UITextField*)textField
{
    self = [super initWithFrame:CGRectMake(0, 250, 320, 40)];
    if (self) {

        inputToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        doneButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Done"
                     style:UIBarButtonItemStyleDone
                     target:self
                     action:@selector(performdone:)];
        nextButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Next"
                     style:UIBarButtonItemStyleBordered
                     target:self
                     action:@selector(nextButtonMethod:)];
        nextButton.tintColor = [UIColor blackColor];
        previousButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Previous"
                     style:UIBarButtonItemStyleBordered
                     target:self
                     action:@selector(previousButtonMethod:)];
        previousButton.tintColor = [UIColor blackColor];

        inputToolBar.barStyle=UIBarStyleDefault;
        UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        [inputToolBar setItems:[NSArray arrayWithObjects:previousButton,nextButton,flexSpace,doneButton,Nil] animated:NO];
        [self addSubview:inputToolBar];
    }
    return self;
}

对工具栏的字段调用:

[self.usernameField setInputAccessoryView:myInputAccessoryView];

我想知道在nextButtonMethod和previousButtonMethod的方法中添加什么来让光标转到数组列表中的下一个字段。我怎么能做到这一点?

3 个答案:

答案 0 :(得分:0)

[textFieldInstance becomeFirstResponder];

将光标放在该字段上。希望您知道如何选择正确的文本字段实例。

答案 1 :(得分:0)

以下框架可能对您有所帮助..

https://github.com/wzbozon/DKKeyboardView 要么     https://github.com/SimonBS/BSKeyboardControls

很简单。

答案 2 :(得分:0)

所以我可能会建议设置视图控制器创建一个xib并使用UITextField的内置setInputAcessoryView。

在我制作的示例中,我创建了一个带有xib的自定义视图控制器,并将其设置为我希望按钮的外观,然后将该视图加载到InputAccessoryView中 通过以这种方式加载它,您可以在键盘停止或成为第一响应者时使用键盘上下动画。

-(void)createFunction{

  [myCustomTextField setDelegate:self];
  [myCustomTextField setInputView:nil]; 
  //Don't have to set this unless you have a custom view
  [myCustomTextField setInputAccessoryView:myCustomInputButtonViewController.view];

}

然后我从AccessoryView视图控制器设置委托方法。因此,当按下按钮时,我可以得到输入并做出相应的响应。

-(void)pressedLeftActionButtonFromEXT{
NSLog(@"Pressed Left Action");
NSInteger nextTag = activeTextField.tag - 1;
// Try to find next responder
UIResponder* nextResponder = [activeTextField.superview viewWithTag:nextTag];
if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
    [activeTextField resignFirstResponder];
  }

}

这是我制作的示例项目中的确切代码行。你可以想象另一个方向恰恰相反。

此外,我的界面中有一个UITextField * activeTextField,以便我可以判断textField何时开启并做出相应的响应。

我希望这会有所帮助。如果这不是很让我知道,我会尽力澄清。我知道我可以解决你遇到的这个问题。

Good Lunk ^^