简体   繁体   中英

UITextField return key on subview

I've got a UIViewController with an additional small UIView I created on top of it (subview). When I click a button this view hovers to the center of the screen. The issue is the i've got a UITextField in the additional UIView and i cannot seem to get the return key to work.

Although I set my IBAction to the event "Editing did end" of the text field, when i click the return key, the IBAction doesn't run.

What am I doing wrong?

确保接口上的UITextFieldDelegate

you just set Your Delegate for example :- yourtextfile.delegate=self; and also dont forget to add delegate method in to .h file

@interface contacts_detailView : UIViewController<UITextFieldDelegate>

and then you delegate textFieldDidEndEditing

- (void)textFieldDidEndEditing:(UITextField *)textField
{
//your new view apear code here 
    [textField resignFirstResponder];
}

Clicking on "Return" doesn't trigger an "Editing did end" event. Only when you'll call resignFirstResponder on the UITextField will you see the event triggered.

What you should do is set your UIViewController as the delegate of the UITextField and implement the method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

First of all, delegate the TextField to the file's owner like this:

yourtextField.delegate = self in your viewDidLoad.

And then add this to the view controller

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

It should work.

There is no need to write more code for key board return. Please write this in your .m file , it will work for any number of text field , no need to write again again for different textfield.

use <UItextfieldDelegate> in your .h file. Then make wiring with fileowner in nib file.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
     [self.view endEditing:YES];
     return YES;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM