Ted
Ted

Reputation: 3885

Programatically adding a tableView, viewDidLoad not called

I would like to use my viewDidLoad function in my tableViewController. How can I make viewDidLoad run in my controller?

tableViewController = [[TableViewController alloc] init];
UITableView *tableView = [[UITableView alloc] init];
tableViewController.view = tableView;
....

Upvotes: 0

Views: 575

Answers (3)

ThorstenC
ThorstenC

Reputation: 1314

tableViewController = [[TableViewController alloc] init];
tableViewController.tableView // This is your newly generated tableview

viewDidLoad will be called after you assign the tableView to another parentview

Upvotes: 0

sergio
sergio

Reputation: 69027

viewDidLoad will be called when the view is actually loaded, which will happen after you present your view controller, by, e.g.:

  1. adding it to a navigation controller,

  2. adding it to a tab bar controller,

  3. presenting it modally.

This is the missing bit in your code. If you explain how you would like to present your view controller, I may help further. Also, have a look at this: Presenting View Controllers.

(I assume the fact that you tried to override the view property of your table view controller was just an attempt "to make things work" -- but you do not need to do anything about that, the view controller will be correctly set up with a table view inside of it).

Upvotes: 1

Marco Pace
Marco Pace

Reputation: 3870

From Apple documentation:

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

So you can try to instantiate it from NIB or overwrite the loadView method. Another step from Apple documentation:

If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.

Upvotes: 2

Related Questions