我相信我无法禁用它,因为我无法以编程方式访问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's
。
b)按预期行事(保存用户输入的数据等),
除了键盘出现时没有被禁用
c)IB>检查员>栏按钮项属性显示:
Identifier = Done
标签= 44
Class = UIBarButtonItem
答案 0 :(得分:2)
你应该只是使用
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;
答案 1 :(得分:1)
您可以收听键盘滑入时发布的通知(UIKeyboardWillShowNotification
):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
然后实施-keyboardWillShow:
。
-(void)keyboardWillShow {
UIButton *button = self.navigationItem.leftBarButtonItem;
button.enabled = NO;
}
要重新启用此按钮,请对UIKeyboardDidHideNotification