开发者

iphone keyboard numeric mode detect

开发者 https://www.devze.com 2023-03-15 08:30 出处:网络
Is is开发者_开发百科 possible to detect when the user pushes the 123 button on a keyboard and moves to the numberic keyboard? I would like to display the inputaccessoryview only when the keyboard is i

Is is开发者_开发百科 possible to detect when the user pushes the 123 button on a keyboard and moves to the numberic keyboard? I would like to display the inputaccessoryview only when the keyboard is in numeric mode, not when it is in the alpha mode.

Thanks!


I'm away from my Mac at the moment so this is all best guess, but ...

UITextField & UITextView both implement the UITextInputTraits protocol meaning they both have a keyboardType property. You should therefore be able to add an observer to this property and when it changes test the change to see if its new type is UIKeyboardTypeNumbersAndPunctuation.

Give this a try:

// 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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消