无法以编程方式隐藏使用IB创建的UIButton [英] Can't programmatically hide UIButton created with IB
问题描述
我的iOS UIButton在我的视图控制器中从IB正确链接到IBOutlet,因为我可以从我的代码更改其标题。即:
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
然而,
[self.myButton setHidden:YES]; //doesn't work
//or
self.myButton.hidden = YES; //doesn't work
发生了什么事?如何让myButton消失?
What's going on? How can I make myButton disappear?
更新:一些额外的信息
这里是与我的UIButton相关的代码:
Here's the code related in to my UIButton:
在我的.h文件中
IBOutlet UIButton *myButton;
-(IBAction)pushedMyButton:(id)sender;
@property (nonatomic,retain) UIButton *myButton;
@synthesize myButton;
- (void)pushedMyButton:(id)sender{
self.myButton.hidden = YES;
}
- (void)dealloc{
[self.myButton release];
}
推荐答案
好的我找到了一个解决方法但我仍然不知道为什么我的原始代码不起作用。我使用Grand Central Dispatch在主队列上调度包含隐藏调用的块,如下所示:
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
});
有趣。我的IBOutlet中的初始代码都没有包含在GCD块中。有什么想法吗?
Interesting. None of the initial code in my IBOutlet was wrapped in GCD blocks though. Any ideas?
这篇关于无法以编程方式隐藏使用IB创建的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!