UITableViewController inside a UIViewController

15,589

Solution 1

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.

Solution 2

You'll want to use an embedded container view.

  1. Drag a "Container View" from Interface Builder sidebar into the view. This adds and links a default "contained" UIView/Controller as well.

  2. Delete the entire UIViewController and View that was automatically added and linked to the container view (as you'll want a Table View Controller instead).

  3. Drag a UITableViewController onto the Storyboard canvas.

  4. Control-Drag from the Container View to the Table View Controller. Select "Embed" to contain the UITableView within the container view.

You're left with the parent view, now containing a UITableView via a Container View. The Controller for the Table View is on the storyboard canvas as well.

Table View Controller inside a Container View

Solution 3

Add UITableViewController to storyboard, And create subclass (new file) of UITableViewController. In Storyboard go to Identity Inspector and in Class field type name of the subclass you created. After that you have to add your app logic based on your requirements.

Solution 4

Create Another UITableViewController in the storyboard, go to its inspector and assign it the same UITableViewController class that you have created before...

Share:
15,589

Related videos on Youtube

Luciano
Author by

Luciano

Updated on September 15, 2022

Comments

  • Luciano
    Luciano over 1 year

    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?

    • Luciano
      Luciano over 10 years
      I know, but I would like to have two different controllers because I already have a UITableViewController for another UITableView and I want to use the same code.
  • Malloc
    Malloc about 10 years
    How would this be done without storyboard? in normal xib files. Any idea? Thanks
  • rdelmar
    rdelmar about 10 years
    @Malloc, You do this in code using the custom container controller api (it has its own section in the UIViewController class reference). Basically, you call addChildViewController: on your parent controller, passing the controller you want to be the child. Then you call didMoveToParentViewController: on the child, passing the parent as the argument. Finally, you add the child's view as a subview of the parent's view.
  • William Daugherty
    William Daugherty over 9 years
    Awesome Idea! I learned something new that has saved me a lot. How do I reference the parent class or child class to transfer my model view?
  • rdelmar
    rdelmar over 9 years
    @BillDaugherty, the child will have a reference to the parent in its parentViewController property, and the parent has a reference to its children through its childViewControllers array.