如何在iPhone中将焦点设置为文本字段? [英] How to set focus to a textfield in iphone?

查看:93
本文介绍了如何在iPhone中将焦点设置为文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过按下文本字段以编程方式将焦点设置到iPhone中的某些文本字段?

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

推荐答案

textField firstresponder,这也会弹出键盘:

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

[self.textField becomeFirstResponder];






还有一些更典型的示例代码可能会有所帮助在您的故事板中有人,


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

,单击其中一个文本字段,并将outlet-delegate设置为相关课程。在该类中,在.h文件中确保声明它是UITextFieldDelegate

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

@interface Login : UIViewController <UITextFieldDelegate>

然后在.m文件中,你可以这样做......

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

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

当用户点击虚拟键盘上的下一步按钮时 - 在'email'字段 - 它会正确地将您移动到密码字段。当用户点击虚拟键盘上的下一步按钮 - 在密码字段上 - 它将正确调用您的登录例程。

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.)

希望它有所帮助!

这篇关于如何在iPhone中将焦点设置为文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆