如何在iOS 5中添加多个UITextfield?
如何创建多个 UITextField 并将其添加到我的控制器?
我可以创建一个,如下所示:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(5,5,100,25)];
tf.borderStyle = UITextBorderStyleRoundedRect;
[tf setReturnKeyType:UIReturnKeyDefault];
[tf setEnablesReturnKeyAutomatically:YES];
[tf setDelegate:self];
[self.view addSubview:tf]
但是我需要为每个 UITextField 执行此操作吗?
模板 UI 控件的最佳方法是什么?
How do I create and add multiple UITextField to my controller?
I can create one, like this:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(5,5,100,25)];
tf.borderStyle = UITextBorderStyleRoundedRect;
[tf setReturnKeyType:UIReturnKeyDefault];
[tf setEnablesReturnKeyAutomatically:YES];
[tf setDelegate:self];
[self.view addSubview:tf]
But do I need to do that for each UITextField?
Whats the best approach to template UI Controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

将其放入循环中,偏移每个文本字段的 Y 位置并标记每个文本字段:
然后在委托方法中根据调用每个委托方法的文本字段的标记执行操作。例如,当用户点击返回键时切换到下一个文本视图:
请记住,在 Objective-C 中向 nil 发送消息不会崩溃,因此当用户点击最后一个文本字段中的返回键作为
UITextField * 时,它会完全正常nextTextField = [self.view viewWithTag:textField.tag + 1];
将返回 nil,并且在 nil 上调用becomeFirstResponder
不会执行任何操作。但是你可以检查 nextTextField 是否为零,然后做其他事情,无论你喜欢什么。Put it in a loop, offset each text field's Y position and tag each text field:
Then in delegate methods perform actions based on tag of the textField that called each delegate method. For example to switch to next text view when user taps return key:
Keep in mind that sending messages to nil in Objective-C will not crash so it will be perfectly fine when user taps return key in last text field as
UITextField *nextTextField = [self.view viewWithTag:textField.tag + 1];
will return nil, and callingbecomeFirstResponder
on nil will do nothing. But you can check if nextTextField is nil and do something else then, whatever you like.