Search code examples
iosviewdidloadreloaddataviewwillappear

IOS [UITableView reloadData] vs new instance of that UITableView class on ViewWillAppear parent class


Im new to iOS programming and I have created my first app for iPad which handles several views. Since I dont know how memory and objects are managed (The app havent crashed but Im trying to prevent them) I have this question.

In general Which is better to have for updating info on the views when navigating to the a child view and returning to the parent or the parent of the parent and so on:

Option 1 instantiate on the Parent viewWillAppear method the Child controller

Parent:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.supportDetailController = [[BIDSupportDetail alloc] init];
}

Option 2 Instantiate on the viewDidLoad Parent method the child controller and on the Child viewWillAppear method call [self.tablewView reloadData]

Parent:

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.supportDetailController = [[BIDSupportDetail alloc] init];
}

Child:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tablewView reloadData]
}

Im using iOS 6.1 and Xcode 4.6.2


Solution

  • If you are not sure about the memory management use ARC. Instantiate the child view controller when it is actually needed. No need to instantiate it in the viewDidLoad or viewWillAppear if it is kind of navigation. Suppose if you have a table view and when you select a cell navigate to the DetailViewController(A normal scenario), then initialize and present the detail view controller from the didSelectRowAtIndexPath.
    Normally reloadTableView in viewWillAppear of childVC is not required by the way it is used in the parentVC to refresh the view when return back, use only of it is required since it is costly in terms of time.