【问题标题】:textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2
【发布时间】: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


【解决方案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];

【讨论】:

    【解决方案2】:

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

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

    您最好为文本字段实现委托,然后您可以检查 shouldBeginEditing/didEndEditing 事件,或者,您可以继承 UITextField 并覆盖 becomeFirstResponder/resignFirstResponder 方法,以便您可以挂钩并实现字段接收和失去焦点时的逻辑。

    【讨论】:

      【解决方案3】:

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

      - (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] 语句。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多