ios 如何将焦点设置到 iphone 中的文本字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7525437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:36:37  来源:igfitidea点击:

How to set focus to a textfield in iphone?

iosuitextfield

提问by Ravi kumar

How do you programmatically set focus to some textfield in iPhone as the result of pressing on the textfield?

作为按下文本字段的结果,您如何以编程方式将焦点设置到 iPhone 中的某个文本字段?

回答by rckoenes

Just make the textField firstresponder, this will also popup the keyboard:

只需使 textField firstresponder,这也会弹出键盘:

[self.textField becomeFirstResponder];


And just some more typical example code which may help someone,

还有一些更典型的示例代码,可以帮助某人,

in your storyboard, click on one of the text fields, and set the outlets-delegate to be the class in question. In that class, in the .h file make sure that you declare that it is a UITextFieldDelegate

在您的故事板中,单击文本字段之一,并将 outlet-delegate 设置为相关类。在那个类中,在 .h 文件中确保你声明它是一个 UITextFieldDelegate

@interface Login : UIViewController <UITextFieldDelegate>

Then in the .m file, you could have this for example...

然后在 .m 文件中,你可以有这个,例如......

-(BOOL)textFieldShouldReturn:(UITextField *)textField
 {
 if ( textField == self.loginEmail ) { [self.loginPassword becomeFirstResponder]; }
 if ( textField == self.loginPassword ) { [self yourLoginRoutine]; return YES; }
 return YES;
 }

When a user clicks the "Next" button on the virtual keyboard - on the 'email' field - it will correctly move you to the password field. When a user clicks the "Next" button on the virtual keyboard - on the 'password' field - it will correctly call your yourLoginRoutine.

当用户单击虚拟键盘上的“下一步”按钮时 - 在“电子邮件”字段上 - 它会正确地将您移动到密码字段。当用户单击虚拟键盘上的“下一步”按钮时 - 在“密码”字段上 - 它会正确调用您的登录程序。

(On the storyboard, on the attributes inspector, notice that you can select the text you want on the Return key ... here "Next" would work nicely on the email field and "Done" would work nicely on the password field.)

(在情节提要上,在属性检查器上,请注意您可以在 Return 键上选择您想要的文本……此处“Next”在电子邮件字段上效果很好,而“Done”在密码字段上效果很好。)

Hope it helps!

希望能帮助到你!

回答by EXC_BAD_ACCESS

Add Delegate to the TextField. You can follow the process detailed in this article: http://www.roseindia.net/tutorial/iphone/examples/iphone-AddTwoTextField.html

将委托添加到 TextField。您可以按照本文中详述的过程进行操作:http: //www.roseindia.net/tutorial/iphone/examples/iphone-AddTwoTextField.html

For changing focus on the fly:

即时改变焦点:

[yourTextField becomeFirstResponder];