ios 如何将 ViewController 添加到导航控制器,如果它包含一个 TableView 作为根?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4789584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:39:33  来源:igfitidea点击:

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

iosuinavigationcontrollertableviewviewcontroller

提问by vuzum

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

我正在尝试将UIViewController(AddProjectViewController)添加到导航控制器 ( navigationController),该控制器的tableView设置为 root,但它不起作用。

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

这就是我设置文件的方式:http: //d.pr/y8rt

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

代码在ProjectsController.m- 请帮忙:(

回答by runmad

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).

推送的视图控制器将自动继承 super(推送它的视图控制器)导航栏(这意味着您可以在子视图控制器中调用 self.navigationController,因为 UINavigationController 只是 UIViewController 的子类(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.

请注意,您需要在 AddProjectViewController 类中创建 UIBarButtonItems。

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

我已更新您的代码并将其上传到此处: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 :)

希望它有所帮助,您需要查看这里的评论,我没有将它们转移到您的项目中。祝你好运 :)