2011-06-24 39 views
0

當用戶按下鍵盤上的123按鈕並移動到數字鍵盤時,是否可以檢測到?我只想在鍵盤處於數字模式時才顯示inputaccessoryview,而不是在alpha模式下。iphone鍵盤數字模式檢測

謝謝!

回答

0

我從我的Mac離開的那一刻,所以這是最好的一切猜測,但...

的UITextField & UITextView中都實現了UITextInputTraits協議意味着他們都有一個鍵盤類型屬性。因此,您應該能夠向此屬性添加觀察者,並在其更改時測試更改以查看其新類型是否爲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 
    } 
} 
+0

我把代碼,在viewDidLoad方法觀測,並在課堂上的實際方法。但它不會觸發它。任何想法爲什麼? – Zsolt

+0

您是否將IBOutlet屬性連接到筆尖的UITextField中? –

+0

我剛剛爲自己試了一下(*這是我第一次有機會*),不幸的是它沒有按預期工作,但我確實說這是最好的猜測。 –