使用StoryBoard建立多个UITextField,比如输入账号、密码。Enter键的响应,有这样的一般需求 Field1->Field2->...->FieldN->Submit

StoryBoard中,各个UITextField,按照顺序与IBOutletCollection建立连接,并且将delegateViewController进行关联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *textFieldCollection;
@end

@implementation ViewController

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSUInteger index = [_textFieldCollection indexOfObject:textField];
if(index != NSNotFound)
{
if(index == _textFieldCollection.count - 1)
{
[textField resignFirstResponder];
[self onLastTextFieldReturn];
}
else
{
UITextField *nextField = _textFieldCollection[index + 1];
[nextField becomeFirstResponder];
}
return NO;
}

return YES;
}

- (void)onLastTextFieldReturn
{
// TODO: Submit
}

@end