以编程方式在UIViewController中添加UINavigationController [英] Programmatically add UINavigationController in UIViewController
问题描述
我目前有一个视图设置如下:
I currently have a view set up as the following:
@interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *mainTableView;
}
@property (nonatomic, retain) UITableView *mainTableView;
正如您所看到的,它内部有一个UITableView,我将所有数据加载到。但是,当我调用以下函数时:
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:
- (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];
}
没有任何反应。出于某种原因,我的 UIViewController
内的 UITableView
并未推送视图。这是因为它不是 UITableViewController
?我尝试将其更改为,但它仍然无效。
nothing happens. For some reason my UITableView
inside of my UIViewController
isn't pushing the view. Is this because it isn't a UITableViewController
? I tried changing it to that, and it still didn't work.
我在这里遗漏了什么吗?
Am I missing something here?
推荐答案
我找到了这篇博客文章对于展示如何在没有Interface Builder的情况下以编程方式创建和使用UINavigationController非常有用。
I found the first parts of this blog post useful for showing how to create and use a UINavigationController programmatically without Interface Builder.
我希望文档和教程给我强调的东西:
Some of the things I wish the docs and tutorials would have stressed to me:
-
当你创建
UINavigationController时
你可以免费获得UIView和UINavigationBar(即你不需要单独添加它们并弄清楚如何将它们连接在一起)。
When you create the
UINavigationController
you 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).
您将 myUINavigationController.view
属性添加为主视图,然后按/ pop UIViewController
s在UINavigationController上,它们将自动显示在 m中可见yUINavigationController.view
UIView。
You add the myUINavigationController.view
property as the "main" view and then push/pop UIViewController
s onto the UINavigationController and they will automatically show up as visible in the myUINavigationController.view
UIView.
将 UIViewController
推送到a UINavigationController
, UIViewController.navigationController
已填写。如果您尚未将视图推送到导航控制器,我我猜这个属性是空的。
When you push a UIViewController
onto a UINavigationController
, the UIViewController.navigationController
is filled in. If you haven't pushed the view onto the navigation controller, I'm guessing that property is empty.
以编程方式将按钮添加到 UINavigationBar的时间/地点
不是在构建 UINavigationController
的时间和地点,而是在推送时加载它们时的个别 UIViewController
到 UINavigationController
。
The time/place to programmatically add buttons to the UINavigationBar
is not when and where you construct the UINavigationController
, but rather by the individual UIViewController
s when they are loaded as you push onto the UINavigationController
.
我只需要在 UINavigationController
的<$ c $中添加一个完成按钮c> UINavigationBar 在第一个 UIViewController
的 viewDidLoad
方法中推送到的UINavigationController
。任何 UIViewController
我按下了第一个视图的顶部,自动在其中有一个后退按钮导航到掩盖的 UIViewController
。
I only had to add a "Done" button to the UINavigationController
's UINavigationBar
in the viewDidLoad
method of the first UIViewController
pushed onto the UINavigationController
. Any UIViewController
I pushed on top of that first view automatically had a "back" button in the to navigate to the covered up UIViewController
.
如果设置<的标题
属性code> UIViewController 然后再把它放在 UINavigationController
的堆栈上。 UINavigationBar
的标题将是您视图的标题。同样,任何后退按钮都会自动命名您要返回的视图,而不是一般地说返回。
If you set the title
property of your UIViewController
before you put it on the UINavigationController
's stack. The UINavigationBar
s 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".
这篇关于以编程方式在UIViewController中添加UINavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!