Trying to disable button using button.enabled = false, but having no luck

痞子三分冷 提交于 2019-12-25 02:24:11

问题


I'm getting frustrated because I am failing to have any control over my bar button items or my UIToolbar. I am trying to disable a UIBarButtonItem, but it continues to respond to touch events. Here is what I have done, the code is so simple I don't know why it isn't working.

in my .h:

  IBOutlet UIBarButtonItem *button;

  @property (nonatomic,retain) IBOutlet UIBarButtonItem *button;

and in .m:

  @synthesize button;

  -(void)function{
      button.enabled = false;
  }

Am I doing something wrong with the viewcontroller delegate? I don't understand why I get no response. Thanks for your help.


回答1:


This is correct:

button.enabled = false;

So are you sure that this method is actually being called? (And if this is your actual code, you have a method called 'function'?)




回答2:


You might be able to achieve this with:

button.target = nil;
button.action = nil;

More:
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html




回答3:


did you connect the button in the xib file to the outlet in the .h file? You may have set the target/IBAction, but you need to setup the reciprocal path. i.e. dragging from the File Owner, which is most likely the viewController, to the button. Then selecting the name of the IBOutlet in your .h file.

Good luck



来源:https://stackoverflow.com/questions/8199540/trying-to-disable-button-using-button-enabled-false-but-having-no-luck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!