如何在iOS 5中添加多个UITextfield?

发布于 01-04 09:00 字数 408 浏览 2 评论 0原文

如何创建多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇2025-01-11 09:00:12

将其放入循环中,偏移每个文本字段的 Y 位置并标记每个文本字段:

for (int i = ; i < numberOfTextFieldsNeeded; i++) {
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5 + 35 * i ,100,25)]; // 10 px padding between each view
    tf.tag = i + 1; // tag it for future reference (+1 because tag is 0 by default which might create problems)
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [tf setReturnKeyType:UIReturnKeyDefault];
    [tf setEnablesReturnKeyAutomatically:YES];
    [tf setDelegate:self];
    [self.view addSubview:tf]
    // don't forget to do [tf release]; if not using ARC
}

然后在委托方法中根据调用每个委托方法的文本字段的标记执行操作。例如,当用户点击返回键时切换到下一个文本视图:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    UITextField *nextTextField = [self.view viewWithTag:textField.tag + 1];
    [nextTextField becomeFirstResponder];
}

请记住,在 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:

for (int i = ; i < numberOfTextFieldsNeeded; i++) {
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5 + 35 * i ,100,25)]; // 10 px padding between each view
    tf.tag = i + 1; // tag it for future reference (+1 because tag is 0 by default which might create problems)
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [tf setReturnKeyType:UIReturnKeyDefault];
    [tf setEnablesReturnKeyAutomatically:YES];
    [tf setDelegate:self];
    [self.view addSubview:tf]
    // don't forget to do [tf release]; if not using ARC
}

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:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    UITextField *nextTextField = [self.view viewWithTag:textField.tag + 1];
    [nextTextField becomeFirstResponder];
}

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 calling becomeFirstResponder on nil will do nothing. But you can check if nextTextField is nil and do something else then, whatever you like.

迷爱2025-01-11 09:00:12

您可以使用界面生成器创建一个视图(大于屏幕适合的尺寸)并以编程方式将其添加到滚动视图。
我认为这是最好的方法。

You can use interface builder to create a view (bigger than the size that the screen fits) and programmatically add it to a scrollView.
This is in my opinion the best approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文