Correct time to call viewDidAppear manually?

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

According to Apple docs, I have to call the method manually:

If a view belonging to the view controller is directly added to the view hierarchy, the view controller will not receive this message. If you insert or add a view to the view hierarchy and have a view controller, you must send the associated control of this message directly.

Question: when is the time to call it manually?

The call from the controller of the parent view -[viewDidAppear] crashes when I try to scroll, because apparently the table view has not actually appeared yet and therefore considers that there is no scrolling for it.

+6
iphone uitableview scroll viewdidappear
source share
5 answers

Calling it from the parent controller, viewDidAppear , is usually the best choice.

If this leads to problems, if the child view controller is not yet fully initialized, another problem may arise. Make sure that the controller of your child view is fully "ready for action" after calling -viewWillAppear (which you can also manually call from the parent -viewWillAppear )

+1
source share

If you use a view controller shell, do not call viewWillAppear: directly. Instead, use – beginAppearanceTransition:animated: and – endAppearanceTransition .

If you are using a custom container controller, use this method to tell your child that his views will appear or disappear. Do not call viewWillAppear :, viewWillDisappear :, viewDidAppear :, or viewDidDisappear: directly.

A call to addSubView will automatically launch viewWillAppear: and viewDidAppear: if the view viewController is a child view controller, so a call to viewWillAppear: will directly invoke the visual view twice. Using beginAppearanceTransition: animated: and - endAppearanceTransition` will suppress automatic behavior, so you only call it once.

+7
source share

In - [viewDidAppear] in the table view, which is really called from the parent view controller - [viewDidAppear], you can call [tableView reloadData], this way you guarantee that the tableView will be loaded and ready.

+1
source share

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

and one of the view controllers loading this path has the following viewWillAppear: (note that this is viewWillAppear , since at the point where viewDidAppear is viewDidAppear , your view is available to the user)

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [wordListTable reloadData]; [wordListTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; } 
0
source share

What error did you get when you called it? You may not have saved your table view correctly.

-one
source share

All Articles