<分区>
我正在尝试将 UIViewController
(AddProjectViewController) 添加到导航 Controller (navigationController
),它有一个 tableView
设置为根, 它不起作用。
这就是我设置文件的方式:http://d.pr/y8rt
代码在 ProjectsController.m
- 请帮忙:(
<分区>
我正在尝试将 UIViewController
(AddProjectViewController) 添加到导航 Controller (navigationController
),它有一个 tableView
设置为根, 它不起作用。
这就是我设置文件的方式:http://d.pr/y8rt
代码在 ProjectsController.m
- 请帮忙:(
最佳答案
好的,所以我先向你解释你做错了什么:
// 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];
推送的 View Controller 将自动继承 super(推送它的 View Controller )导航栏(这意味着您可以在 subview Controller 中调用 self.navigationController,因为 UINavigationController 只是 UIViewController 的一个子类(因此也是UITableViewController)。
这是您需要做的:
// 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];
但是,对于您要尝试执行的操作,以模态方式呈现 View Controller 会更好,这意味着您必须将它保存在导航 Controller 中。方法如下:
// 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];
请注意,您需要在 AddProjectViewController 类中创建 UIBarButtonItems。
我已更新您的代码并将其上传到此处:http://dl.dropbox.com/u/5445727/Zum.zip
希望对您有所帮助,您需要查看此处的评论,我没有将它们转移到您的项目中。祝你好运:)
关于ios - 如果 ViewController 包含 TableView 作为根目录,如何将它添加到导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789584/