iphone键盘数字模式检测

时间:2011-06-24 12:41:56

标签: iphone events keyboard

当用户按下键盘上的123按钮并移动到数字键盘时,是否可以检测到?我想仅在键盘处于数字模式时显示inputaccessoryview,而不是在alpha模式下显示。

谢谢!

1 个答案:

答案 0 :(得分:0)

我现在离开了我的Mac,所以这是最好的猜测,但是......

UITextField& UITextView都实现了UITextInputTraits协议,这意味着它们都具有keyboardType属性。因此,您应该能够向此属性添加一个观察者,并在其更改时测试该更改,以查看其新类型是否为UIKeyboardTypeNumbersAndPunctuation。

尝试一下:

// In your header
@property (nonatomic, retain) IBOutlet UITextField *textField

// In your init or viewDidLoad function
[textField addObserver:self forKeyPath:@"keyboardType" options:NSKeyValueObservingOptionNew context:nil]

// Somewhere in your class add the following
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (textfield.keyboardType == UIKeyboardTypeNumbersAndPunctuation) {
        // This is where you'd add your accessoryView
    } else {
        // And this is where you'd remove it when the keyboardType changes to 
        // anything other than UIKeyboardTypeNumbersAndPunctuation
    }
}