Correct time to call viewDidAppear manually?
Asked Answered
M

4

7

I have a UITableViewController in my app, which is added to the view hierarchy directly. After the view appears, I want to scroll to a specific cell. My solution would be to call the code for scrolling in -[viewDidAppear].

According to Apple's docs I have to call the method manually:

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

The question is: When is the right time to call it manually?

Calling it from the parent view controller's -[viewDidAppear] leads to a crash when I try to do the scrolling because apparently, the table view actually didn't yet appear and therefore thinks it has no sections to scroll to.

Morrissey answered 22/9, 2009 at 17:53 Comment(0)
C
0

Calling it from the parent controller's -viewDidAppear is usually your best bet.

If this results in problems if the child view controller isn't fully initialized yet, you may have another problem. Make sure your child view controller is completely "ready for action" after -viewWillAppear is called (which you can also call manually from the parent's -viewWillAppear)

Chela answered 22/9, 2009 at 17:59 Comment(2)
You should absolutely never call these life cycle events directlyFenner
If your view controller is not in the root view controller heirachy then those life cycle events will never be called, so you absolutely need to call them manually if you want to use them. They're just a function, not the boogy man.Articulate
I
21

If you are using view controller containment don't call viewWillAppear: directly. Instead use – beginAppearanceTransition:animated: and – endAppearanceTransition.

If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.

Calling addSubView will automatically trigger viewWillAppear: and viewDidAppear: if the view's viewController is a child view controller, therefore calling viewWillAppear: directly will trigger view will appearance method twice. Using beginAppearanceTransition:animated:and– endAppearanceTransition` will suppress the automatic behaviour so you only get it called once.

Idolater answered 30/7, 2014 at 9:22 Comment(1)
thanks my friend, after spending whole day for a bug, your answer saved my dayElwandaelwee
X
1

In -[viewDidAppear] on the tableview, called indeed from the parent view controller's -[viewDidAppear], you can call [tableView reloadData], this way you ensure that the tableView is loaded and ready.

Xylo answered 22/9, 2009 at 17:59 Comment(0)
C
0

Calling it from the parent controller's -viewDidAppear is usually your best bet.

If this results in problems if the child view controller isn't fully initialized yet, you may have another problem. Make sure your child view controller is completely "ready for action" after -viewWillAppear is called (which you can also call manually from the parent's -viewWillAppear)

Chela answered 22/9, 2009 at 17:59 Comment(2)
You should absolutely never call these life cycle events directlyFenner
If your view controller is not in the root view controller heirachy then those life cycle events will never be called, so you absolutely need to call them manually if you want to use them. They're just a function, not the boogy man.Articulate
D
0

This is how I manually call viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear: here

and one of the view controllers I load this way, has the following viewWillAppear: (note that this is viewWillAppear since at the point that viewDidAppear is called, your view is viewable by the user)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [wordListTable reloadData];
    [wordListTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
Dowser answered 23/9, 2009 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.