Tony Armstrong
Tony Armstrong

Reputation: 638

Using Storyboard while Programmatically-Creating UITableViewController

I want to programmatically create a UITableViewController, using a TableView that I create in a storyboard where I name the storyboard TableViewCont.storyboard.

Here's the code I was trying to use in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   NSBundle *bundle = [NSBundle mainBundle];
   UITableViewController *tvc = [[UITableViewController alloc] initWithNib:@"TableViewCont" bundle:bundle];
   self.window.rootViewController = tvc;
   return YES;
}

There's no syntax error reported but the TableViewController doesn't show when I run the app on my device. I know UITableViewController functions differently than UIViewController when you initialize with initWithNib:bundle: but I'm lost as to why this code doesn't work...

Upvotes: 0

Views: 425

Answers (1)

manujmv
manujmv

Reputation: 6445

You can't use an initWithNib in your storyboard. This is for the xib files.

To initialize a controller from the storyboard, you have to use instantiateViewControllerWithIdentifier. See the below example

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TableViewCont" bundle:nil]; 

UITableViewController *tvc = (UITableViewController *)[storyboard instantiateViewControllerWithIdentifier:@"your identifier"];

Dont forget to put identifier for your tableview controller in the storyboard

Upvotes: 1

Related Questions