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?
Update:
I did a bit of digging around and turns out the @synthesize
keyword is no longer needed starting from XCode 4.4.