2

我相信我无法禁用它,因为我无法以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) 是UIBarButttonItem从 IB 库添加到导航栏的,该导航栏位于具有一些UITextField's.
b) 行为符合预期(保存用户输入的数据等),
除了在键盘出现时没有被禁用。
c) IB > Inspector > Bar Button Item Attributes 显示:
Identifier = Done
Tag = 44
Class =UIBarButtonItem

4

2 回答 2

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;
于 2010-11-09T16:46:13.720 回答
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

于 2010-11-09T16:46:56.997 回答