UIKeyboard 返回按钮操作与 UITextView resignFirstResponder 不同
我有一个 UITextView 供用户输入一些文本。当使用以下方式调用 UITextView 时:
[textView becomeFirstResponder];
我添加一个 rightBarButtonItem,它允许用户使用以下方式关闭 UIKeyboard:
[textView resignFirstResponder];
我想知道是否可以连接 UIKeyboard 的返回键来执行某些操作,但也可以自行关闭它。 Action是保存一些添加到textView中的数据。
那么如何实现操作,其中两者都调用 resignFirstResponder 但做不同的事情。
这是我使用的代码。
-(void)viewDidLoad {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:self action:@selector(cancelEditing)] autorelease];
}
- (void)textViewDidEndEditing:(UITextView *)textview {
[self saveSomeData];
}
- (void)cancelEditing {
[commentTextView resignFirstResponder];
}
我可能知道,在这两种情况下,resignFirstResponder 也会调用 textViewDidEndEditing 吗?
提前致谢!
I have a UITextView for the user to enter som text. When the UITextView is called with:
[textView becomeFirstResponder];
I add a rightBarButtonItem, which lets the user to dismiss the UIKeyboard with:
[textView resignFirstResponder];
I wonder if I can wire the return key of the UIKeyboard to do some action but also to dismiss it self. Action is to save some data added to the textView.
So how can implement to actions, where both call resignFirstResponder but do different things.
Here is the code i use.
-(void)viewDidLoad {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:self action:@selector(cancelEditing)] autorelease];
}
- (void)textViewDidEndEditing:(UITextView *)textview {
[self saveSomeData];
}
- (void)cancelEditing {
[commentTextView resignFirstResponder];
}
I this possible do I understand that resignFirstResponder also calls textViewDidEndEditing in both cases.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 UITextView,您需要实现 UITextViewDelegate 并使用以下方法:
检查 text 参数,如果它等于“\n”,则可以关闭键盘。
编辑:正在谈论 TextView,保留在下面以防万一需要。
将您的控制器设置为 UITextField 的委托。然后实现:
在这个方法中你可以辞去第一响应者。
UITextField 还应该自动启用 Return 键。
For a UITextView you need to implement the UITextViewDelegate and use the method:
Check the text parameter, if it is equal to "\n", then you can dismiss the keyboard.
EDIT: Was talking about TextView, keeping below just in case it's needed.
Set your controller to be the delegate of the UITextField. Then implement:
In this method you can resign first responder.
The UITextField should also Auto-enabled the Return key.