Question

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

Was it helpful?

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.

OTHER TIPS

If I understand correctly, you want to update your parent, when the child does something. For example:

  1. Parent is has list of Bars, which are displayed on a UITableView.
  2. The child allows you to create a Bar.
  3. When you create the Bar, and you are back to the Parent, the Parent will show it on the UITableView.

In this case, I prefer to not rely on viewWillAppear or viewDidAppear in the Parent. I normally create a reference from the parent to the Child, by using a Protocol and a weak reference inside the child. So when you need to actually update the parent, you just pass the new created object to the parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top