如何禁用“完成”当键盘滑入时,导航栏中的按钮?

发布于 2024-10-01 13:15:59 字数 1660 浏览 0 评论 0原文

我相信我无法禁用它,因为我无法以编程方式访问该 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-10-08 13:15:59

你应该只是使用

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;

You should just be using

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;
寄风 2024-10-08 13:15:59

您可以收听键盘滑入时发布的通知 (UIKeyboardWillShowNotification):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

然后实现 -keyboardWillShow:

-(void)keyboardWillShow {
    UIButton *button = self.navigationItem.leftBarButtonItem;
    button.enabled = NO;
}

要再次重新启用该按钮,请对 UIKeyboardDidHideNotification 执行相同的操作

You can listen to a notification (UIKeyboardWillShowNotification) posted when the keyboard slides in:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

Then implement -keyboardWillShow:.

-(void)keyboardWillShow {
    UIButton *button = self.navigationItem.leftBarButtonItem;
    button.enabled = NO;
}

To reenable the button again, do the same for the UIKeyboardDidHideNotification

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文