When adding subviews to a view, do I @synthesize it, or create it inside
loadView method
What is the correct way of programmatically adding subviews to a view,
A) using @property:
// SampleViewController.h
@interface SampleViewController : UIViewController
@property (nonatomic, weak) UITableView *subTableView;
@end
// SampleViewController.m
@synthesize subTableView = _subTableView;
or B) in loadView:
// SampleViewController.m
- (void)loadView
{
UITableView *subTableView = [[UITableView alloc]
initWithFrame:self.view.bounds];
[self.view addSubview:subTableView];
}
Again, this is assuming I am not using IB at all. What is the difference
between the two (because in practice both seems to work)?
Additional question, if I have both the A) and B) codes in
SampleViewController.m, why does XCode allow me to use subTableView as
variable name in B, even though I already used that name in the
@synthesize part of the code?
No comments:
Post a Comment