键盘更改时是否有任何通知(例如从NumberPad到默认值)

时间:2012-03-27 20:44:33

标签: objective-c ios cocoa-touch uikeyboard uikeyboardtype

我想知道如何在键盘更改时收到通知。我要做的是为4型和4型键盘添加一个DONE按钮。 5(NumberPad和PhonePad),一切正常,除非我使用默认KB类型从TextField转换,没有触发KeyboardDidAppear的通知。

这是我得到的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:nil]; 

}

然后我为当前的KB类型和正在编辑的当前TextField添加了一个Property:

#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentKBType = textField.keyboardType;
    self.curTextField = textField;
    return YES;
}

然后我决定是否根据当前的KB类型添加DONE按钮:

- (void)keyboardDidShow:(NSNotification *)note {
    if (self.currentKBType == 4 || self.currentKBType == 5) {
        [self addButtonToKeyboard];
    }
}

问题是显示键盘时会触发通知,但是当键盘发生变化时会触发(从一个TextField转换到指定不同KB类型的另一个TextField。

有什么建议吗?我错过了什么吗?

1 个答案:

答案 0 :(得分:6)

弄明白这一点。采取了一点逻辑,但它完美无瑕。这是我做的: 为:

添加了私有属性
@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;

然后在TextField委托方法中添加以下逻辑:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.currentKBType = textField.keyboardType;
    if (textField.keyboardType == 4 || textField.keyboardType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }

    self.curTextField = textField;
    return YES;
}

在我在viewDidLoad中签署VC的KeyboardDidShowNotification中:

- (void)keyboardDidShow:(NSNotification *)note {
    if (self.currentKBType == 4 || self.currentKBType == 5) {
        if (!doneButtonDisplayed) {
            [self addButtonToKeyboard];
        }
    } else {
        if (doneButtonDisplayed) {
            [self removeButtonFromKeyboard];
        }
    }
}

这些方法中引用的两种方法:

- (void)addButtonToKeyboard {

    // create custom button
    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [doneButton setImage:[UIImage imageNamed:@"DoneNormal.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneHL.png"] forState:UIControlStateHighlighted];

    [doneButton addTarget:self action:@selector(resignKeyboard) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    if ([[[UIApplication sharedApplication] windows] count] > 1) {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for(int i=0; i<[tempWindow.subviews count]; i++) {
            keyboard = [tempWindow.subviews objectAtIndex:i];
            // keyboard found, add the button
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) {
                [keyboard addSubview:doneButton];
                self.doneButtonDisplayed = YES;
            }
        }

    }
}

- (void)removeButtonFromKeyboard {  
    [doneButton removeFromSuperview];
    self.doneButtonDisplayed = NO;
}

最后,每当触摸完成按钮时调用的resignKeyboard方法:

-(void)resignKeyboard {
    [self.curTextField resignFirstResponder];
    self.doneButtonDisplayed = NO;
}

每当显示NumberPad或PhonePad类型键盘时,这将添加Done按钮,并从其他键盘类型中删除它(仅当它已添加时)。

经过测试并且效果很好。