Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewController inside a UIViewController

I'm new to iOS and objective-C and I'm having some trouble in understanding how controllers work.

In my code I have a UIViewController (with my custom controller assigned by storyboard) and inside it, together with other objects, I want to have a table handled by a different controller. What is the right way to do this?

like image 673
Luciano Avatar asked Dec 11 '13 15:12

Luciano


People also ask

Is UITableViewController a subclass of UIViewController?

The UITableViewController class itself is a subclass of UIViewController. Here's the class hierarchy of an example table view controller that displays contact information: an instance of ContactsTableViewController.

What is UITableViewController?

A view controller that specializes in managing a table view.

What is the role of UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is the difference between Table View and Table View Controller?

A TableViewController is a ViewController with a TableView built in. This will have the delegate methods needed already declared and setup. This VC is already a TableView delegate and datasource. It cannot be resized.


1 Answers

You can make that table view controller a child view controller of your UIViewController.

In the storyboard, you can do this easily by dragging a container view into your controller's view, and that will give you a child view controller automatically.

You'll want to:

  • delete the child view controller it gives you (it's just a UIViewController)
  • drag out a table view controller
  • control drag from the container view to the table view controller
  • choose "embed".

If you need to get a reference to this table view controller from the UIViewController, you can do that in prepareForSegue -- the table view controller will be the segue's destination view controller, and prepareForSegue will be called right after the controllers are instantiated.

like image 169
rdelmar Avatar answered Oct 12 '22 11:10

rdelmar