Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't programmatically hide UIButton created with IB

My iOS UIButton is correctly linked from IB to an IBOutlet in my view controller, as I can change its title from my code. Ie:

[self.myButton setTitle:@"new title" forState:UIControlStateNormal]; //works

However,

[self.myButton setHidden:YES]; //doesn't work
//or
self.myButton.hidden = YES; //doesn't work

What's going on? How can I make myButton disappear?

Update: some additional info

Here's the code related in to my UIButton:

in my .h file

IBOutlet UIButton *myButton;
-(IBAction)pushedMyButton:(id)sender;
@property (nonatomic,retain) UIButton *myButton;

in my .m file

@synthesize myButton;
- (void)pushedMyButton:(id)sender{
    self.myButton.hidden = YES;
}
- (void)dealloc{
    [self.myButton release];
}
like image 717
DavidD Avatar asked Dec 21 '10 10:12

DavidD


2 Answers

Ok I found a workaround that works but I still don't know why my original code wasn't working in the first place. I used Grand Central Dispatch to dispatch a block containing the hide call on the main queue, like this:

dispatch_async(dispatch_get_main_queue(), ^{
    self.myButton.hidden = YES; //works
});

Interesting. None of the initial code in my IBOutlet was wrapped in GCD blocks though. Any ideas?

like image 91
DavidD Avatar answered Oct 22 '22 07:10

DavidD


That should work, try rename it and hide it just to check that there aren't two buttons on top of each other.

like image 43
VdesmedT Avatar answered Oct 22 '22 05:10

VdesmedT