How to navigate through textfields (Next / Done Buttons)

来源:互联网 发布:烤箱daogrs 知乎 编辑:程序博客网 时间:2024/05/29 14:23

how can i navigate through all my text fields with the "Next" Button on the iPhone Keyboard.

The last text field should close the Keyboard.

I setup in the IB the Buttons (Next / Done) but now im stuck.

I implemented the textFieldShouldReturn action but now the Next and Done Buttons close the Keyboard.

Thanks for your help!

我怎么才能通过点击iPhone键盘上的“NEXT”按钮来导航, 

最后一个textField应该是要关闭键盘

我在ib里面已经设置了按钮的next/done

我实现了textFieldShouldReturn方法, 但是next/done按钮没有关闭键盘


up vote17down voteaccepted

In Cocoa for Mac OS X you have the next responder chain, where you can ask the text field what control should have focus next. This is what makes tabbing between text fields work. But since iPhone do not have a key board, only touch, this concept has not survived the transition to Cocoa Touch.

This can be easily done anyway, with two assumptions:

  1. All "tabbable" UITextFields are on the same parent view.
  2. Their "tab-order" is defined by the tag property.

Assuming this you can override textFieldShouldReturn: as this:

在mac os里面,有下一个响应者链,你可以看textField的焦点是不是“next”


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 14.0px Consolas; background-color: #eeeeee}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 14.0px Consolas; color: #808080; background-color: #eeeeee}span.s1 {color: #2b91af}span.s2 {color: #800c07}span.s3 {color: #000000}span.s4 {color: #0d1c8b}

-(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 NO; // We do not want UITextField to insert line-breaks.

}



原创粉丝点击