在 UINavigationbar 中添加完成按钮

发布于 2024-11-09 14:42:15 字数 131 浏览 0 评论 0原文

当用户触摸特定文本字段或文本视图时,如何向 UINavigationbar 添加完成按钮?

或者更好的方法是检测键盘何时显示,然后显示按钮。

我希望使用“完成”按钮来关闭键盘,就像在标准 Notes 应用程序中一样。

How does one add a done button to a UINavigationbar when the user touches a specific textfield or textview?

Or would a better way be to detect when the keyboard is showing, and then display the button.

I would like the done button to dismiss the keyboard like in standard Notes application.

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

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

发布评论

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

评论(2

财迷小姐 2024-11-16 14:42:17

您应该采用委托协议,因为我相信您在这方面具有优势。您可以这样做 -

- (void)textViewDidBeginEditing:(UITextView *)textView {
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:textView
                                                                                action:@selector(resignFirstResponder)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
}

但如果您观察通知,您将无法知道哪个是第一响应者。当然,如果您只需要担心一个对象,那么这并不是什么大问题。

You should adopt the delegate protocol as I believe you are at an advantage there. You can do this –

- (void)textViewDidBeginEditing:(UITextView *)textView {
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:textView
                                                                                action:@selector(resignFirstResponder)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
}

But if you were to observe the notification, you can't know which one is the first responder. Of course, it is not much of a problem if you have only one object to worry about.

白况 2024-11-16 14:42:16

您可以尝试与此类似的操作:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{       
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(doneEditing)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

并且还

- (void)textViewDidBeginEditing:(UITextView *)textView 
{       
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(doneEditing)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

可以根据需要自定义以下内容

- (void)doneEditing {
    [[self view] endEditing:YES];
}

,然后删除 - (void)textFieldDidEndEditing:(UITextField *)textField- (void) 中的按钮textViewDidEndEditing:(UITextView *)textView

只要记住设置委托即可!

You could try something similar to this:

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{       
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(doneEditing)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

and also

- (void)textViewDidBeginEditing:(UITextView *)textView 
{       
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                target:self
                                                                                action:@selector(doneEditing)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

with the following customized as you like

- (void)doneEditing {
    [[self view] endEditing:YES];
}

then remove the button in - (void)textFieldDidEndEditing:(UITextField *)textField and also in - (void)textViewDidEndEditing:(UITextView *)textView

Just remember to set up the delegates!

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