ran*_*dom 10 iphone sdk uikeyboard
我在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中运行代码的区别
任何人都可以解释行为上的差异吗?
@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
Run Code Online (Sandbox Code Playgroud)
- (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");
}
Run Code Online (Sandbox Code Playgroud)
我相信从 iOS 3.2 开始,在两个文本字段之间切换时不再触发 UIKeyboardWillHideNotification 和 UIKeyboardWillShowNotification。基本上,仅当键盘实际显示或隐藏时才会触发通知,并且由于从一个文本字段切换到另一个文本字段不会隐藏键盘,因此不会触发该事件。
在 iOS 3.2 之前,每当您更改字段时都会触发事件。新方法可以说更正确,但它确实使您尝试做的事情更具挑战性。
您可能最好实现文本字段的委托,然后您可以检查 shouldBeginEditing/didEndEditing 事件,或者,您可以子类化 UITextField 并重写 comeFirstResponder/resignFirstResponder 方法,以便您可以挂钩它们并在以下情况下实现您的逻辑:场域接收和失去焦点。
| 归档时间: |
|
| 查看次数: |
12571 次 |
| 最近记录: |