我有一个UITextField,当你点击它时会弹出一个键盘。我已经使用故事板添加了UITextField,现在我想在键盘弹出时禁用按钮。我怎么能这样做?
禁用按钮:[self.howButton setEnabled:NO];
但是我在哪里放这个?
答案 0 :(得分:3)
你可以在notifacticon
显示时添加你的键盘,然后在隐藏时禁用按钮,然后启用按钮
在init中添加观察者
- (void)init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
}
实现显示和隐藏方法
- (void)keyboardWillHide
{
[self.howButton setEnabled:YES];
}
- (void)keyboardWillShow
{
[self.howButton setEnabled:NO];
}
当类是dealloc
时,最后删除观察者- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
答案 1 :(得分:0)
注册UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
在keyboardWillShow方法中禁用按钮。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
如果需要,您可以通过注册上述通知重新启用该按钮。