【发布时间】:2011-02-03 07:34:03
【问题描述】:
【问题讨论】:
标签: iphone
【问题讨论】:
标签: iphone
1.定义完成按钮(=返回键):
textField.returnKeyType = UIReturnKeyDone;
2.添加动作监听器:
[textField addTarget:self action:@selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
3.定义动作事件:
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
玩得开心!
编辑:
在这里您可以找到详细说明如何在 UITextField 键盘上方添加带有 Next 和 Previous 的 Toolbar: http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/
EDIT2:
现在,我有一个非常棒的示例:“此视图扩展了 UITextView,在与此 UITextView 关联的键盘顶部添加了一个带有«完成»按钮的工具栏”
我检查了代码,它比第一个示例要容易得多:
EDIT3:
嗯,不,我不测试代码。但我现在要测试它!
1.问题:正确的初始化。如果我在 IB 中添加 UITextView,则会调用 initWithCoder:
- (id)init {
NSLog(@"init");
if (self = [super init]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
NSLog(@"initWithCoder");
if (self = [super initWithCoder:decoder]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
NSLog(@"initWithFrame");
if (self = [super initWithFrame:frame]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
2.问题:没有前缀为“UIKeyboard”的视图:
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
NSLog(@"keyboardWindow = %@", keyboardWindow);
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(@"keyboard = %@", keyboard);
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
// THERE'S NO VIEW 'UIKeyboard'!!!
}
}
}
代码不起作用,对不起...我不知道为什么没有视图“UIKeyboard”...也许第一个示例在这一点上会对您有所帮助,您可以构建自己的解决方案。
【讨论】: