如何以编程方式禁用/启用 UIBarButtonItem [英] How to programmatically disable/enable a UIBarButtonItem
问题描述
我目前有一个名为 continueButton
的按钮,它已分配给 Storyboard 文件中的 UIBarButtonItem 按钮.我已经声明了按钮:
I currently have a button called continueButton
that has been assigned to a UIBarButtonItem button in a Storyboard file. I've declared the button as such:
- (IBAction)continueButton;
当视图加载时,我希望按钮禁用自身,这样就没有用户输入,除非调用一个命令来重新启用按钮的用户输入.我该怎么做呢?我正在尝试使用函数 [continueButton setEnabled:YES];
以编程方式禁用/启用按钮,但它无法正常工作.
When the view loads, I want the button to disable itself, so that there can be no user input unless a command is called that re-enables user input for the button. How would I go about doing this? I'm trying to use the function [continueButton setEnabled:YES];
to disable/enable the button programmatically, but it doesn't work properly.
推荐答案
您尚未将 UIBarButtonItem
暴露给您的代码.为了使控件的属性可访问,它要么需要连接到 IBOutlet
(XIB 和代码之间的桥梁),如 hw731 所说,要么首先以编程方式创建.操作更像是将方法绑定到控件引发的事件.
You haven't exposed the UIBarButtonItem
to your code. For the properties of the control to be accessible it either needs to be connected to an IBOutlet
(bridge between XIB and code) as hw731 said, or created programmatically in the first place. An action is more like binding a method to an event raised by the control.
如果您在建立连接时看不到 Outlet
或 Outlet
集合的选项,则您可能位于 .m
文件中.h
文件.
If you cant see the option for an Outlet
or Outlet
collection when making the connection its likely you are in the .m
file instead of the .h
file.
这篇关于如何以编程方式禁用/启用 UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!