如何禁用“完成”当键盘滑入时,导航栏中的按钮?
我相信我无法禁用它,因为我无法以编程方式访问该 UIBarButttonItem
(使用 viewWithTag 或 rightBarButtonItem )。
有什么建议(除了添加没有 IB 的接口之外)?
作为测试,我还尝试以编程方式添加按钮(在导航栏的左侧),但它没有显示在导航栏中。
相关代码(在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) 是从 IB 库添加到导航栏的 UIBarButttonItem
,该导航栏位于具有一些 UITextField
的滚动视图中。
b)按预期运行(保存用户输入的数据等),
除了键盘出现时不会被禁用。
c) IB>检查员>栏按钮项目属性显示:
标识符 = 完成
标签 = 44
类 = UIBarButtonItem
I believe I can't disable it because I can't access that UIBarButttonItem
programmatically
(with either viewWithTag or rightBarButtonItem
).
Any suggestions (short from adding the interface without IB)?
As a test, I also tried adding a button programmatically (on the left of the nav bar), but it did not display in the nav bar.
RELEVANT CODE (In 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];
}
DETAILS
I would think this is a common case because that Done button:
a) is a UIBarButttonItem
added from IB Library to navigation bar that is in a Scroll View that has some UITextField's
.
b) behaves as expected (to save the user-entered data etc),
except for not getting disabled when keyboard appears.
c) IB > Inspector > Bar Button Item Attributes shows:
Identifier = Done
Tag = 44
Class = UIBarButtonItem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该只是使用
You should just be using
您可以收听键盘滑入时发布的通知 (
UIKeyboardWillShowNotification
):然后实现
-keyboardWillShow:
。要再次重新启用该按钮,请对
UIKeyboardDidHideNotification
执行相同的操作You can listen to a notification (
UIKeyboardWillShowNotification
) posted when the keyboard slides in:Then implement
-keyboardWillShow:
.To reenable the button again, do the same for the
UIKeyboardDidHideNotification