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

发布于 2024-10-13 19:53:53 字数 1513 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-10-20 19:53:53

好的,所以我首先向您解释一下您做错了什么:

// 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];

推送的视图控制器将自动继承超级(推送它的视图控制器)导航栏(这意味着您可以在子级中调用 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];

但是,对于您想要做的事情,最好以模态方式呈现视图控制器,这意味着您必须将其保存在导航控制器中。具体方法如下:

// 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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文