【发布时间】:2011-02-28 02:26:56
【问题描述】:
我在 UIView 上有多个文本字段。
我为 textFieldShouldBeginEditing 方法中的前一个 textField 辞职,在该方法中执行以下事件序列
UIKeyboardWillHideNotification 接收到对应于前一个字段的键盘被隐藏的那个字段。
方法 textFieldShouldBeginEditing 返回 YES 然后
UIKeyboardWillShowNotification 在当前字段的键盘显示位置接收。
但是,在 OS 3.2 中,即使 textFieldShouldBeginEditing 返回 YES,也不会收到当前字段的 UIKeyboardWillShowNotification。
该逻辑适用于操作系统
有什么想法我可能做错了吗?
下面列出了我的部分代码(xib 中只有两个文本字段)。
我需要在keyboardWillShow和keyboardWillHide执行一组操作看看OS 3.2和OS
谁能解释行为上的差异?
.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");
}
【问题讨论】:
-
能否给我源代码,我还是一头雾水?
-
您能解释一下您在 textFieldShouldBeginEditing 方法中编写的代码要达到什么目的吗?此外,当您为 keyboardWillShow 和 keyboardWillHide 添加观察者时,请尝试为对象传递 nil 而不是 self.view.window
-
如果您尝试通过在顶部添加一个条来更改您的键盘,这很常见,请尝试在您的 UITextFields 上设置 inputAccessoryView 属性。但是,如果您尝试在 UIKeyboardWillShow 上滚动文本字段,则不必自己编写该代码。 CocoaControls 上有很多项目可以提供帮助,例如 IBAForms。
标签: iphone sdk uikeyboard