强制隐藏键盘/resignFirstResponder

发布于 2024-12-17 21:40:19 字数 206 浏览 2 评论 0原文

我正在开发一个应用程序,它有一个 tableView ,每个单元格的右侧都有一个 textField (有超过 20 个单元格)。 我已经为除最后一行之外的每一行创建了自定义单元格。 最后一行只有一个按钮。

现在我想在单击按钮时调用 resignFirstResponder
我该怎么办请帮忙?

i'm working on an app which has a tableView with a textField in the right side of its each cell(there are more than 20 cells).
i've created custom cell's for each row except for the last one.
In the last row there is only a button.

Now i want to call resignFirstResponder on the button's click.
What should i do Please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

念三年u 2024-12-24 21:40:19

您必须跟踪哪个单元格中的哪个文本字段有第一响应者,并像这样退出它。

[myCellTextField resignFirstResponder];

You will have to keep track of which textfield in which cell has the first responder and resign it like this.

[myCellTextField resignFirstResponder];
淡紫姑娘! 2024-12-24 21:40:19

您可能想用键盘跟踪文本字段。在控制器中实现 协议,并将控制器设置为每个文本字段的委托。像这样编写 textFieldDidBeginEditing: 方法,设置一个名为 currentTextField 的实例变量:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    currentTextField = [textField retain];
}

然后,在按钮的操作中运行 [currentTextField resignFirstResponder]

You probably want to keep track of the text field with the keyboard. Implement the <UITextFieldDelegate> protocol in your controller, and set the controller as each of the text fields' delegates. Write the textFieldDidBeginEditing: method like so, setting an instance variable called currentTextField:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    currentTextField = [textField retain];
}

Then, in your action for the button run [currentTextField resignFirstResponder].

若沐 2024-12-24 21:40:19

Aopsfan的答案可能是迄今为止最好的解决方案。但是,要添加它(因为我无法发表评论),请记住取消分配该对象:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (currentTextField != nil) {
        [currentTextField release];
    }
    currentTextField = [textField retain];
}

最好还是使用@property's和@synthesize,以便运行时可以为您进行内存管理。

[ViewController].h

@property (nonatomic, retain) UITextField* currentTextField;

[ViewController].m

@synthesize currentTextField = _currentTextField;

- (void)viewDidLoad|Appear {
    self.currentTextField = nil;
}

- (void) dealloc {
    [_currentTextField release], _currentTextField = nil;
    ...
    [super dealloc];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentTextField = textField;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentTextField) {
        [self.currentTextField resignFirstResponder];
    }
}

Aopsfan's answer is probably the best solution so far. However, to add to it (as I cannot post comments), do remember to deallocate the object:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (currentTextField != nil) {
        [currentTextField release];
    }
    currentTextField = [textField retain];
}

Better still use @property's and @synthesize so the runtime can do the memory management for you.

[ViewController].h

@property (nonatomic, retain) UITextField* currentTextField;

[ViewController].m

@synthesize currentTextField = _currentTextField;

- (void)viewDidLoad|Appear {
    self.currentTextField = nil;
}

- (void) dealloc {
    [_currentTextField release], _currentTextField = nil;
    ...
    [super dealloc];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentTextField = textField;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentTextField) {
        [self.currentTextField resignFirstResponder];
    }
}
友欢 2024-12-24 21:40:19

我认为此链接会对您有所帮助:

Objective C:带有按钮的ResignFirstResponder

提供一些代码,这样可以更轻松地帮助您。

希望这对您有帮助。 :)

I think this link will help you:

Objective C: ResignFirstResponder with a button

Provide some code so that, it will be easier to help u.

Hope this helps you. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文