は、どちらもKEYBOARDTYPEプロパティを持っている意味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
}
}
、viewDidLoadメソッドではオブザーバ、およびクラス内の実際の方法:
は、この試してみて。それはそれを引き起こさない。なぜどんなアイデア? – Zsoltあなたのペン先のUITextFieldにIBOutletプロパティを接続しましたか? –
私は自分自身でこれを試しました(*私が持っていた最初の機会です*)。残念ながら期待通りに動作しませんでしたが、これは最高の推測でした。 –