How to set focus to a textfield in iphone? [closed]

我与影子孤独终老i 提交于 2019-12-20 09:46:55

问题


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


回答1:


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,

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>

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;
 }

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

Hope it helps!




回答2:


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

For changing focus on the fly:

[yourTextField becomeFirstResponder];


来源:https://stackoverflow.com/questions/7525437/how-to-set-focus-to-a-textfield-in-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!