textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2 文本字段应该开始编辑 + 键盘即将显示通知 + 操作系统 3.2

10

在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");
}

你能给我源代码吗?我还是很困惑。 - vodkhang
1
你能解释一下你在textFieldShouldBeginEditing方法中编写的代码想要实现什么吗?此外,在添加keyboardWillShow和keyboardWillHide的观察者时,请尝试将对象的值设为nil,而不是self.view.window。 - ThE uSeFuL
如果您想在键盘显示时添加一个常见的顶部栏来更改键盘,可以尝试在UITextFields上设置inputAccessoryView属性。但是,如果您想在UIKeyboardWillShow时将textFields滚动到一边,则不必自己编写代码。有很多项目可以帮助您,例如CocoaControls上的IBAForms - zelk
3个回答

3

我相信从iOS 3.2开始,当在两个文本字段之间切换时,UIKeyboardWillHideNotification和UIKeyboardWillShowNotification不再被触发。基本上,只有当键盘实际显示或隐藏时,通知才会触发,因此从一个文本字段切换到另一个文本字段并不会隐藏键盘,因此事件不会触发。

在iOS 3.2之前,这些事件在您更改字段时会触发。新的方式可以说更正确,但它确实使您所尝试做的事情变得更具挑战性。

您最好实现文本字段的代理,然后您可以检查shouldBeginEditing/didEndEditing事件,或者您也可以子类化UITextField并重写becomeFirstResponder/resignFirstResponder方法,以便在字段接收和失去焦点时实现自己的逻辑。


1

我认为你正在尝试在特定文本字段上更改键盘类型。不要像你现在这样追踪它,而是简单地使用这两种方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;

第一种方法是在您触摸文本字段以进行编辑时调用的。 在这里,您可以编写键盘更改代码

EG: If textfield is of type 1
       set Keyboard Type to alphanumeric.
    Else if textfield is of type 2
       set Keyboard Type to numeric only.

然后第二种方法是在您按下屏幕键盘上的RETURN键时调用。 在这里,您可以为任何传入的文本字段控件编写[textfield resignFirstResponder]语句。

希望这有所帮助.. :) 干杯!


1

当键盘出现时,NotificationCenter 调用该方法。 如果不起作用,请将对象设置为 nil。

[[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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接