我正在尝试重新创建类似Safari中使用的弹出键盘。
我可以通过在视图上放置工具栏和适当的按钮来视觉上重现它,但是我无法找到任何方法在用户触摸“完成”按钮后关闭键盘。
我正在尝试重新创建类似Safari中使用的弹出键盘。
我可以通过在视图上放置工具栏和适当的按钮来视觉上重现它,但是我无法找到任何方法在用户触摸“完成”按钮后关闭键盘。
有几件事情需要记住。开发者最容易忘记设置的第一部分是textField的代理(delegate)。
如果你正在使用Interface Builder,则必须记住将textField的代理设置为文件所有者(file Owner)。
如果您没有使用Interface Builder,则确保将textfield的代理设置为self。我还包括了returnType。例如,如果textField被称为gameField:
gameField.delegate = self;
gameField.returnKeyType = UIReturnKeyDone;
你还必须为你的ViewController实现UITextFieldDelegate。
@interface YourViewController : UIViewController <UITextFieldDelegate>
最后需要使用 textFieldShouldReturn 方法并调用 [textField resignFirstResponder]
-(BOOL) textFieldShouldReturn:(UITextField*) textField {
[textField resignFirstResponder];
return YES;
}
所有的文本字段都会使用这个相同的方法,所以您只需要进行一次设置即可。只要为textField设置了代理,实现了UITextFieldDelegate接口,您就可以添加textFieldShouldReturn方法并调用resignFirstResponder设置即可。
你尝试过了吗:
[viewReceivingKeys resignFirstResponder];
viewReceivingKeys
是接收文本输入的UIView吗?
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"%@ textFieldShouldReturn", [self class]);
[theTextField resignFirstResponder];
// do stuff with the text
NSLog(@"text = %@", [theTextField text]);
return YES;
}
在 iPhone Cocoa 文档中的 UITextFieldDelegate textFieldShouldReturn:
如果你想要关闭UITextField
的键盘而不是UITextView
,请确保你的问题表述清晰。如果是这样,请在接口文件中标记你的类为UITextFieldDelegate
。
@interface MyController: UIViewController <UITextFieldDelegate> {
UITextField *activeTextField;
// ...remainder of code not show ...
}
然后您需要实现以下两个委托方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
activeTextField = textField;!
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
activeTextField = nil;
[textField resignFirstResponder];
return YES;
}
UITextView
,那么事情会变得更加复杂。 UITextViewDelegate
协议缺少与textFieldShouldReturn:
方法等效的方法,这可能是因为我们不应该期望回车键是一个信号,表明用户希望停止编辑多行文本输入对话框中的文本(毕竟,用户可能希望通过按返回键插入换行符)。UITextView
无法使用键盘放弃第一响应者身份的方法。通常的方法是在UITextView
呈现弹出式键盘时,在导航栏中放置一个完成按钮。当点击此按钮时,它会要求文本视图放弃第一响应者身份,然后关闭键盘。UITextView
外部时,UITextView
放弃第一响应者身份。为此,您需要子类化UIView
以接受触摸,然后指示文本视图在用户点击视图本身之外时放弃第一响应者身份。#import <UIKit/UIKit.h>
@interface CustomView : UIView {
IBOutlet UITextView *textView;
}
@end
接下来,在实现中,实现 touchesEnded:withEvent:
方法并要求 UITextView
辞去第一响应者的身份。
#import "CustomView.h"
@implementation CustomView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void) awakeFromNib {
self.multipleTouchEnabled = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches began count %d, %@", [touches count], touches);
[textView resignFirstResponder];
[self.nextResponder touchesEnded:touches withEvent:event];
}
@end
一旦你添加了类,你需要保存所有更改,然后进入Interface Builder并点击你的视图。在Utility面板中打开Identity inspector,并将你nib文件中的视图类型更改为你的CustomView
而不是默认的UIView
类。然后在Connections Inspector中,将textView
outlet拖到UITextView
上。这样做后,重新构建应用程序后,触摸非活动UI元素外部将会关闭键盘。但请注意,如果你要子类化的UIView
在其他UI元素的“后面”,这些元素将在到达UIView层之前截取触摸事件。因此,虽然这种解决方案很优雅,但它只能在某些情况下使用。在许多情况下,你将不得不采取强制添加一个Done按钮到导航栏来关闭键盘的方法。
使用导航控制器,在完成后弹出视图?
例如,我使用这样的代码来滑动关于框:
[[self navigationController] presentModalViewController:modalViewController animated:YES];
然后,当关于对话框中的按钮被点击时,我使用这个来将其关闭:
[self.navigationController dismissModalViewControllerAnimated:YES];