在UIView上我有多个文本字段。
在textFieldShouldBeginEditing方法中,我为以前的textField辞职,执行以下事件序列:
接收到与该字段对应的UIKeyboardWillHideNotification,表示先前字段的键盘已隐藏。
textFieldShouldBeginEditing方法返回YES,然后
接收到UIKeyboardWillShowNotification,当前字段的键盘将显示。
但是,在OS 3.2中,即使textFieldShouldBeginEditing返回了YES,也不会收到当前字段的UIKeyboardWillShowNotification。
这个逻辑在OS < 3.2下有效。
有任何想法我可能做错了什么?
以下是我的代码的一部分(xib中只有两个文本字段)。
我需要在keyboardWillShow和keyboardWillHide执行一系列操作。查看在OS 3.2和OS < 3.2中运行代码的差异。
谁能解释行为上的区别?
.h
@interface ExampleViewController : UIViewController
{
IBOutlet UITextField *numericTextField;
IBOutlet UITextField *alphaTextField;
UITextField *lastTextField;
int lastCursorPos;
int cursorPosition;
NSMutableArray *textFields;
}
@property (nonatomic, retain) UITextField *lastTextField;
@property (nonatomic, retain) NSMutableArray *textFields;
@end
.m
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
[self.textFields insertObject:alphaTextField atIndex:0];
[self.textFields insertObject:numericTextField atIndex:1];
cursorPosition = 1;
[numericTextField becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self setEditing:NO animated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
int index;
for(UITextField *aField in self.textFields){
if (textField == aField){
index = [self.textFields indexOfObject:aField];
}
}
if(index>=0 ){
lastCursorPos = cursorPosition;
self.lastTextField = [self.textFields objectAtIndex:lastCursorPos-1];
cursorPosition = index +1;
}
[self.lastTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notif {
NSLog(@"Inside keyboardWillShow");
}
- (void)keyboardWillHide:(NSNotification *)notif {
NSLog(@"Inside keyboardWillHide");
}