UiTextField and resignFirstResponder

旧街凉风 提交于 2019-12-05 03:18:37

问题


I have a weird setup.

I have a View called View1 and a ViewController call viewController1

in IB, View1 is a child of the ViewController1.

Inside View1, I created using code a UITextField and added as a subview.

In my Viewcontroller, I have viewController1 : UIViewController <UITextFieldDelegate>

Now.. I would like to resign the keyboard when the text field within View1 gets the "Done" button in the keyboard so I have

- (BOOL)textFieldShouldReturn:(UITextField *)TEXTFIELD {
    [TEXTFIELD resignFirstResponder];
}

Now the question is, how do I make the connection between TEXTFIELD (defined in my viewcontroller) and the textfield defined in my View?

Do I have to do something in interfaceBuilder?

maybe I am totally off here....

Some hints greatly appreciated


回答1:


Kendall is right. You need to set your viewController1 as the delegate of your text field.

In your comment above, I think you're misunderstanding the textFieldShouldReturn method. The UITextField* is passed in as a parameter to this method, so that the delegate can see which field wants to return, and decide whether to allow it to. In your case, you only have one textfield, so just resign first responder and return YES (my code is below).

Try putting a breakpoint on this textFieldShouldReturn function. Is it getting called?

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



回答2:


Connect the delegate property of the text field to your view controller in IB (should be File's Owner for that view). Also note in the view controller header that you conform to the protocol.

Or you can just set the texfield.delegate property to self in the viewDidLoad method of the view controller.



来源:https://stackoverflow.com/questions/684579/uitextfield-and-resignfirstresponder

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