当键盘滑入键盘时,如何禁用NAV栏的“完成”按钮?
-
30-09-2019 - |
题
我相信我无法禁用它,因为我无法访问 UIBarButttonItem
以编程方式
(使用ViewWithTag或 rightBarButtonItem
).
有任何建议(添加没有IB的界面的简短)?
作为测试,我还尝试通过编程添加一个按钮(在NAV栏的左侧),但没有在NAV栏中显示。
相关代码(在 MyEditorViewControler.m
):
- (void)textFieldDidBeginEditing:(UITextField *)sender { //successfully executes when keyboard slides in
UINavigationItem *item = self.navigationItem; //item = 0x6420e0 OK. (value at debugger breakpoints)
UIBarButtonItem *doneButton4 = (UIBarButtonItem *) [self.view viewWithTag:44]; //doneButton4 = 0x0, not OK.
doneButton4.enabled = NO;
}
- (void)textFieldDidEndEditing:(UITextField *)sender { //successfully executes when keyboard slides out.
...
UIButton* doneButton = (UIButton *)[self.view viewWithTag:44]; //Attempt to re-enable button.
doneButton.enabled = YES;
}
- (void)viewDidLoad { //Attempt to programmatically add a *left* button to the nav bar. Result: Button does not display in nav bar.
....
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
[leftBarButtonItem release];
}
细节
我认为这是一个常见的情况,因为那个完成按钮:
a)是 UIBarButttonItem
从IB库中添加到带有一些滚动视图中的导航栏 UITextField's
.
b)行为如预期的(保存用户输入的数据等),
除了出现键盘时未被禁用。
c)IB> Inspector> BAR按钮项属性显示:
标识符=完成
标签= 44
class = UIBarButtonItem
解决方案
您可以收听通知(UIKeyboardWillShowNotification
)键盘滑动时发布:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
然后实现 -keyboardWillShow:
.
-(void)keyboardWillShow {
UIButton *button = self.navigationItem.leftBarButtonItem;
button.enabled = NO;
}
要再次重新确定按钮,请对 UIKeyboardDidHideNotification
其他提示
你应该只使用
UIBarButtonItem *doneButton = self.navigationItem.leftBarButtonItem;
doneButton.enabled = YES;
//Both of these should work, you shouldn't need any type of IBOutlets for this
UINavigationItem *item = self.navigationItem;
UIBarButtonItem *doneButton = item.leftBarButtonItem;
不隶属于 StackOverflow