How to add a ViewController to a Navigation Controller, if it contains a TableView as root? [closed]
Questions concerning problems with code you've written must describe the specif开发者_高级运维ic problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this questionI'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 :(
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 :)
精彩评论