Reputation: 519
I would like to add a UIButton
in a UIView
from my xib file.
In my xib, I declare a UIView
outlet named "content" with interface builder: here is the generated code :
@property (weak, nonatomic) IBOutlet UIView *content;
I would like to add a button, here is the code for this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 8, 141.0, 56.0);
[button setBackgroundImage:[UIImage imageNamed:@"TAB_NORMAL"]
forState:UIControlStateNormal];
button.frame = CGRectMake(0, 8, 56.0, 141.0);
[self.content addSubview:button];
If I put this code in -viewDidLoad
method, it works, but if I put it in a function called later, the button is never displayed.
What could be the reason ?
Thanks for your help.
Upvotes: 0
Views: 753
Reputation: 41622
If your UIView content
is a top level item, that is, not contained in your main view - then it has to be strong
. Only items that are not top-level can use weak
, since the containing view would keep a strong reference to them. I also assume that content
was immediately added to the primary view, which kept a strong reference to it (it in viewDidLoad happened before ARC nil'ed out content
.
If for some reason changing the property to strong
does not fix the problem, then for sure add a NSLog (or assert) to insure that content
is non-nil.
Upvotes: 1