如何使用iPhone键盘上的“下一步”按钮浏览所有文本字段?最后一个文本字段应该关闭键盘。我已经设置了IB按钮(下一个/完成),但现在我被卡住了。我实现了textFieldShouldRe转折点操作,但是现在下一个按钮关闭键盘。
3 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
所有“可表” UITextField
S在同一个父视图上。 它们的“制表符顺序”由Tag属性定义。
-(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.}
SWIFT 4.0
func textFieldShouldReturn(_ textField: UITextField) -> Bool { let nextTag = textField.tag + 1 // Try to find next responder let nextResponder = textField.superview?.viewWithTag(nextTag) as UIResponder! if nextResponder != nil { // Found next responder, so set it nextResponder?.becomeFirstResponder() } else { // Not found, so remove keyboard textField.resignFirstResponder() } return false}
如果文本字段的SuperView将是UITableViewCell,那么下一个响应程序将是
let nextResponder = textField.superview?.superview?.superview?.viewWithTag(nextTag) as UIResponder!
临摹微笑
TA贡献1982条经验 获得超2个赞
UITextField+Extended.h
@interface UITextField (Extended)@property(retain, nonatomic)UITextField* nextTextField;@end
UITextField+Extended.m
#import "UITextField+Extended.h"#import <objc/runtime.h>static char defaultHashKey;@implementation UITextField (Extended)- (UITextField*) nextTextField { return objc_getAssociatedObject(self, &defaultHashKey); }- (void) setNextTextField:(UITextField *)nextTextField{ objc_setAssociatedObject(self, &defaultHashKey, nextTextField, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }@end
UITextField *textField1 = ...init your textfieldUITextField *textField2 = ...init your textfieldUITextField *textField3 = ... init your textfield textField1.nextTextField = textField2;textField2.nextTextField = textField3;textField3.nextTextField = nil;
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { UITextField *next = theTextField.nextTextField; if (next) { [next becomeFirstResponder]; } else { [theTextField resignFirstResponder]; } return NO; }
- 3 回答
- 0 关注
- 639 浏览
添加回答
举报
0/150
提交
取消