Trying to disable button using button.enabled = fa

2019-09-07 04:59发布

问题:

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