How to add a ViewController to a Navigation Controller, if it contains a TableView as root?

22,972

OK, so I'll just explain to you what you're doing wrong first:

// You're not allocating the view here.
AddProjectViewController *nextController = addProjectViewController;
// When allocated correctly above, you can simple push the new controller into view
[self.navigationController pushViewController: (UIViewController *)addProjectViewController animated:YES];

The pushed view controller will automatically inherit super's (the view controller that's pushing it) navigation bar (which means you can make calls on self.navigationController in the child view controller, since UINavigationController is simply a subclass of UIViewController (and so is UITableViewController).

Here's what you need to do:

// Allocate AddProjectViewController
AddProjectViewController *addProjectViewController = [[AddProjectViewController alloc] init];
// Adds the above view controller to the stack and pushes it into view
[self.navigationController pushViewController:addProjectViewController animated:YES];
// We can release it again, because it's retained (and autoreleases in the stack). You can also choose to autorelease it when you allocate it in the first line of code, but make sure you don't call release on it then!
[addProjectViewController release];

However, for what you're trying to do, it would be much better to present the view controller modally, which means you will have to hold it inside a navigation controller. Here's how:

// Allocate AddProjectViewController
AddProjectViewController *addProjectViewController = [[AddProjectViewController alloc] init];
// Create a navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addProjectViewController];
// Release the view controller that's now being retained by the navigation controller
[addProjectViewController release];
// Adds the above view controller to the stack and present it modally (slide from bottom)
[self presentModalViewController:navigationController animated:YES];
// Release the navigation controller since it's being retained in the navigation stack
[navigationController release];

Note that you need to create UIBarButtonItems in your AddProjectViewController class.

I have updated your code and uploaded it here: http://dl.dropbox.com/u/5445727/Zum.zip

Hope it helps, you'll need to look at the comments here, I didn't transfer them to your project. Good luck :)

Share:
22,972
vuzum
Author by

vuzum

I run the creative agency @Vuzum as co-founder and CEO. We do exciting projects for our customers as well as in-house awesomeness: @flabell @themesbell @blogvio And now I'm learning Objective-C, which is why most of my questions are related to that! :)

Updated on September 23, 2020

Comments

  • vuzum
    vuzum over 3 years

    I'm trying to add a UIViewController (AddProjectViewController) to a Navigation Controller (navigationController), which has a tableView set as root, and it does not work.

    This is how I have the files setup: http://d.pr/y8rt

    The code is in ProjectsController.m - please help :(

  • vuzum
    vuzum over 13 years
    Thank you so much! I'm going over the entire code you sent, thank you again!
  • vuzum
    vuzum over 13 years
    Oh yes, it makes sense now! I will go with the Modal View as well, it makes more sense. Thank you a million!
  • vuzum
    vuzum over 13 years
    I have one more question if you can help - how would I go about accessing the tableView now, from the AddProjectViewController? I added the addProject method there, but when I try to call: [super.tableView reloadData]; It doesn't work. Also, when I comment this particular line, the app crashes and the data doesn't get saved. :(
  • vuzum
    vuzum over 13 years
    I put the files here, again please check - (void)addProject:(id)sender in AddProjectViewController here: d.pr/5INn
  • runmad
    runmad over 13 years
    You have four options: 1) Make a delegate for AddProjectsViewController that ProjectsController inherits. 2) Use NSNotifications 3) call reloadData on the tableView in viewWillAppear 4) Since you're using Core Data, there's actually a delegate method that will fire when the data is changed (deleted records, etc). Add this delegate method in your ProjectsController and reload the tableview inside this method. Personally, I would go with option #4, since you have already setup all the CD stuff, you just need to add a free delegate method and you're pretty much done!