Adrienne
Adrienne

Reputation: 2680

How do I connect a TableViewController to my main ViewController?

Right now I have:

  1. TableViewController - displays a table that is populated with images and has UITableViewDelegate methods
  2. ViewController- empty except for default viewDidLoad() fn

When I run my program, the screen is blank, presumably because my ViewController has nothing in it.

How do I make it so that it displays my TableViewController?

I have a hunch that I should use prepareForSegue, but am confused because when does prepareForSegue ever get called? I've read it is called before the viewDidLoad(). In that case, should I add a prepareForSegue function in my ViewController that directs to my TableViewController?

Upvotes: 0

Views: 643

Answers (2)

trevorj
trevorj

Reputation: 2049

Click the ViewController in your storyboard, delete it, and then select your table view controller in the storyboard (click the little yellow icon on the left where the three icons appear right above the controller so that there is a blue outline around the controller). In the Attributes inspector in the utilities panel on the right in Xcode, check "Is Initial View Controller". Sounds like you don't need the other view controller at all.

If you want to segue to the table view controller from your view controller, add a button or some other UI control to the view controller, control-drag from that control to your table view controller, and then select a segue type. prepareForSegue() is called right before segueing; it's a way for you second view controller (in your case your table view controller) to get data that it may need from the first view controller.

Upvotes: 1

Charles
Charles

Reputation: 4538

You can set the root view controller in the app delegate didFinishLaunchingWithOptions method. Something like:

TableViewController *tableViewController = [[TableViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = tableViewController;
[self.window makeKeyAndVisible];

should do the trick.

Upvotes: 0

Related Questions