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
}
}
精彩评论