textFieldShouldBeginEditing

textFieldShouldBeginEditing

我在UIView上有多个文本字段。

我辞去了textFieldShouldBeginEditing方法中的上一个textField,在该方法中执行以下事件序列

接收到与该字段相对应的

  • UIKeyboardWillHideNotification,该字段隐藏了前一个字段的键盘。
  • 方法textFieldShouldBeginEditing返回YES,然后
  • 在显示当前字段的键盘的位置接收到
  • UIKeyboardWillShowNotification。

  • 但是,在OS 3.2中,即使textFieldShouldBeginEditing返回YES,也不会收到当前字段的UIKeyboardWillShowNotification。

    该逻辑适用于OS
    有什么想法我可能做错了吗?

    在我的代码的一部分下面列出(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");
    }
    

    最佳答案

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

    在iOS 3.2之前的版本中,每当您更改字段时,事件都会触发。新方法可以说是更正确的,但是它确实使您尝试做的事情更具挑战性。

    您可能最好为文本字段实现委托(delegate),然后可以检查shouldBeginEditing/didEndEditing事件,或者,可以子类化UITextField并重写beginFirstResponder/resignFirstResponder方法,以便可以在发生以下情况时挂接它们并实现逻辑这些领域会失去焦点。

    10-07 23:40