ios - 如果 ViewController 包含 TableView 作为根目录,如何将它添加到导航 Controller ?

标签 ios uinavigationcontroller tableview viewcontroller

<分区>

我正在尝试将 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/

相关文章:

Swift 导入字典到 tableviewcell

java - 如何更改 JavaFX 中的 TableView 框架颜色?

java - 将数据库中的数据显示到tableView中

ios - Sharekit 中 Linkedin 请求错误

ios - 如何将操作从主队列转移到后台释放主队列

ios - 当前导航 Controller 来自另一个导航 Controller

ios - 导航 Controller 后退按钮不出现?

ios - Firebase queryOrderedByChild() 方法不提供排序数据

ios - 检查 UIView 是否在 Self.View 边界内

c# - 弹出式导航 - iOS