Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:59:14+00:00 2026-06-14T10:59:14+00:00

I need to display UITableViewController programmatically from existing view. I have a MyAppViewController and

  • 0

I need to display UITableViewController programmatically from existing view.

I have a MyAppViewController and the button on its view should open a new UITableViewController. But I cannot seem to get it working so far. Here is how UITableViewController looks like:

@interface SecondViewController : UITableViewController{}

and on the main view I add table view controller using the following:

SecondViewController vc2 = [[SecondViewController alloc] init];
vc2.view.frame = {...}
[self.view addSubview: vc2.view];

This is how I would normally add a simple view controller so I thought UITableViewController is the same.

The result I get is empty cells on the entire screen. This is because of the delegate not set, I assume, but where would I set it? So my confusion is the delegate setters on UITableViewController view controller and displaying UITableViewController correctly from the main view controller’s view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:59:15+00:00Added an answer on June 14, 2026 at 10:59 am

    EDIT

    This answer is outdated. The correct way to add a view controller’s view as a subview of another view controller is to implement a container view controller.

    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

    ORIGINAL ANSWER

    Your pattern is a bit out of the ordinary. The most common way to do what you are describing is to initialize and present SecondViewController. If that’s in fact what you want I highly recommend you peruse Apple’s docs on how to create, customize, and present UIViewController and UITableViewController.

    If you know all of that and really want to do something custom then read on.

    Instead you are creating an instance of SecondViewController and rather than presenting it, you are adding it’s view to the current view controllers view. If you know what you’re trying to accomplish and this is in fact your desired result then go for it.

    There is more than one way to configure this general pattern. I’ll stick to the simplest way in this example.

    1) MainViewController (MVC) should retain the SecondViewController (SVC) instance in a property as long as it is needed.

    2) SVC is a subclass of UITableViewController so by default, it is set as the dataSource and delegate for it’s UITableView. That means you need to implement the UITableViewDataSource and UITableViewDelegate methods in SVC for your table to be populated with data. Assuming MVC knows what data needs to go into the table, it should pass this to SVC. The simplest way is to define a property on SVC that can be set in MVC at initialization time.

    3) Assuming there is a way to dismiss the table after it has been presented, you’ll want MVC to do that. Basically, MVC would remove SVC‘s view from it’s superview and then set the SVC property to nil.

    Here’s some quick psuedo-code. I wrote the bare minimum as an example.

    // MainViewController.h
    //
    #import "SecondViewController.h"
    
    @interface MainViewController : UIViewController
    @property (nonatomic, strong) SecondViewController *svc;
    @end
    
    // MainViewController.m
    //
    #import "MainViewController.h"
    
    @implementation MainViewController
    
    // init and configure views w/ init, loadView, viewDidLoad, etc
    
    // present SecondViewController
    - (void)presentSecondViewController:(id)sender {
        self.svc = [[SecondViewController alloc] init];
        // this example uses an array as the SVC data
        self.svc.tableData = @[@"first", @"second", @"third", @"fourth"];
        self.svc.view.frame = self.view.bounds;
        [self.view addSubview:self.svc.view];
    }
    
    // dismiss SecondViewController
    - (void)dismissSecondViewController:(id)sender {
        if (self.svc) {
            [self.svc.view removeFromSuperview];
            self.svc = nil;
        }
    }
    
    // SecondViewController.h
    //
    @interface SecondViewController : UITableViewController
    @property (nonatomic, strong) NSArray *tableData;
    @end
    
    // SecondViewController.m
    //
    @implementation SecondViewController
    
    // init and configure views w/ init, loadView, viewDidLoad, etc
    
    // override tableData getter to create empty array if nil
    - (NSArray *)tableData
    {
        if (!tableData) {
            _tableData = @[];
        }
        return _tableData;
    }
    
    // override tableData setter to reload tableView
    - (void)setTableData:(NSArray *)tableData
    {
        _tableData = tableData;
        [self.tableView reloadData];
    }
    
    // implement UITableViewDelegate and UITableViewDataSource methods using
    // the self.tableData array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display image and one button on fancybox popup but I can't
I need to display array of images in a single view which have various
I have a UITableViewController. I need to display a table cell followed by a
i am developing one application with map view i need display the weather depends
I need to display the parent from the first Element of input.xml Also I
I need to display a certain view when an activity starts, and when the
I have a UITableViewController that when loaded gets data from a web-service and stores
I have a List of string and I need display the values of this
I need to display a pop up from my jsp. The code is as
I need to display pop ups in my app. So I have user controls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.