简体   繁体   English

UITextField可以在UIButton touch上辞退第一响应者

[英]UITextField to resign first responder on UIButton touch

I have a UITableView with some cells containing UITextField objects and others with UIButton objects. 我有一个UITableView,其中一些单元格包含UITextField对象,另一些单元格包含UIButton对象。 When one of the textFields is first responder (keyboard showing) i am having trouble resigning its first responder status. 当textFields中的一个是第一响应者(显示键盘)时,我无法退出其第一响应者状态。 When a user clicks or touches a button in another cell, the textField is still not resigning even though i am calling [textField resignFirstResponder] on the 'did end on exit' and 'Editing did end' events. 当用户单击或触摸另一个单元格中的按钮时,即使我在“退出时结束”和“编辑结束时”事件上调用[textField resignFirstResponder][textField resignFirstResponder]退出。 I am also calling [button becomeFirstResponder] when the button is clicked. 单击按钮时,我也调用[button becomeFirstResponder] I guess it has to do with objects being in different cells. 我想这与对象位于不同单元格中有关。 any ideas? 有任何想法吗?

EDIT: 编辑:

i am resigning the first responder like this: 我要这样辞职第一响应者:

-(IBAction)endEditing:(id)sender {

    UITextField *textField = (UITextField *)sender;
    [textField resignFirstResponder];
}

the endEditing: method is called on both UITextField events: 'did end on exit' and 'Editing did end'. 在两个UITextField事件上都调用了endEditing:方法:“退出时结束”和“编辑完成”。

Where are you calling this [textField resignFirstResponder] and where is this textField declared? 您在哪里调用此[textField resignFirstResponder]并在何处声明此textField?

Proper way would be to declare an instance variable: 正确的方法是声明一个实例变量:

UITextField *activeTextField;

And once any text field becomes active set it as activeTextField. 一旦任何文本字段变为活动状态,请将其设置为activeTextField。 Then when you press the button do [activeTextField resignFirstResponder]; activeTextField = nil; 然后,当您按下按钮时,请执行[activeTextField resignFirstResponder]; activeTextField = nil; [activeTextField resignFirstResponder]; activeTextField = nil;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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