ios 以编程方式在 UIViewController 中添加 UINavigationController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4400901/
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
Programmatically add UINavigationController in UIViewController
提问by joshholat
I currently have a view set up as the following:
我目前有一个视图设置如下:
@interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *mainTableView;
}
@property (nonatomic, retain) UITableView *mainTableView;
As you can see, it has a UITableView inside of it that I load all of my data to. However, when I call the following function:
如您所见,它内部有一个 UITableView,我将所有数据加载到其中。但是,当我调用以下函数时:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SingleBlogViewController *viewController = [[SingleBlogViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
//[self presentModalViewController:viewController animated:YES];
[viewController release];
}
nothing happens. For some reason my UITableViewinside of my UIViewControllerisn't pushing the view. Is this because it isn't a UITableViewController? I tried changing it to that, and it still didn't work.
没发生什么事。出于某种原因,我的UITableView内心UIViewController并没有推动观点。这是因为它不是UITableViewController? 我试过改成那个,还是不行。
Am I missing something here?
我在这里错过了什么吗?
回答by David Blevins
I found the first parts of this blog postuseful for showing how to create and use a UINavigationController programmatically without Interface Builder.
我发现这篇博文的第一部分对于展示如何在没有 Interface Builder 的情况下以编程方式创建和使用 UINavigationController 很有用。
Some of the things I wish the docs and tutorials would have stressed to me:
我希望文档和教程对我强调的一些事情:
When you create the
UINavigationControlleryou get a UIView and UINavigationBar for free (i.e. you don't need to separately add them and figure out how to hook them together).You add the
myUINavigationController.viewproperty as the "main" view and then push/popUIViewControllers onto the UINavigationController and they will automatically show up as visible in themyUINavigationController.viewUIView.When you push a
UIViewControlleronto aUINavigationController, theUIViewController.navigationControlleris filled in. If you haven't pushed the view onto the navigation controller, I'm guessing that property is empty.The time/place to programmatically add buttons to the
UINavigationBaris not when and where you construct theUINavigationController, but rather by the individualUIViewControllers when they are loaded as you push onto theUINavigationController.I only had to add a "Done" button to the
UINavigationController'sUINavigationBarin theviewDidLoadmethod of the firstUIViewControllerpushed onto theUINavigationController. AnyUIViewControllerI pushed on topof that first view automatically had a "back" button in the to navigate to the covered upUIViewController.If you set the
titleproperty of yourUIViewControllerbefore you put it on theUINavigationController's stack. TheUINavigationBars title will be the title of your view. As well any "back" buttons will automatically name the view you are returning to instead of generically saying "back".
当您创建时,
UINavigationController您会免费获得一个 UIView 和 UINavigationBar(即您不需要单独添加它们并弄清楚如何将它们连接在一起)。您将该
myUINavigationController.view属性添加为“主”视图,然后 push/popUIViewControllers 到 UINavigationController 上,它们将自动显示为在myUINavigationController.viewUIView 中可见。当您将 a 推
UIViewController到 a 上时UINavigationController,将UIViewController.navigationController填满。如果您还没有将视图推到导航控制器上,我猜该属性是空的。以编程方式将按钮添加到 的时间/地点
UINavigationBar不是您构造 的时间和地点UINavigationController,而是UIViewController当您推入UINavigationController.我只需要添加一个“完成”按钮到
UINavigationController的UINavigationBar在viewDidLoad第一的方法UIViewController推到UINavigationController。任何UIViewController被推我上面说第一种观点的自动曾在“后退”按钮,导航到掩盖了UIViewController。如果您在
title将您的属性UIViewController放入UINavigationController的堆栈之前设置它。该UINavigationBar的头衔将是您的视图的标题。同样,任何“后退”按钮都会自动命名您要返回的视图,而不是一般地说“后退”。
回答by niceamitsingh
Please visit "How to add UINavigationController Programmatically"
请访问“如何以编程方式添加 UINavigationController”
Please visit above link & get all information regarding UINavigationController.
请访问上面的链接并获取有关 UINavigationController 的所有信息。
UINavigationController is a subclass of UIViewController, but unlike UIViewController it's not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.
UINavigationController 是 UIViewController 的一个子类,但与 UIViewController 不同的是,它通常不适合您进行子类化。这是因为导航控制器本身很少在导航栏的视觉效果之外进行自定义。UINavigationController 的实例可以在代码中或在 XIB 文件中相对容易地创建。

