Programmatically add UINavigationController in UIViewController(以编程方式在 UIViewController 中添加 UINavigationController)
问题描述
我目前的视图设置如下:
@interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{UITableView *mainTableView;}@property (nonatomic, 保留) UITableView *mainTableView;
如您所见,它内部有一个 UITableView,我可以将所有数据加载到其中.但是,当我调用以下函数时:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {SingleBlogViewController *viewController = [[SingleBlogViewController alloc] init];[self.navigationController pushViewController:viewController Animation:YES];//[self presentModalViewController:viewController Animation:YES];[viewController 发布];}
什么都没有发生.出于某种原因,我的 UIViewController
内部的 UITableView
没有推送视图.这是因为它不是 UITableViewController
吗?我试着改成那个,还是不行.
我错过了什么吗?
我找到了这篇博文有助于展示如何在没有 Interface Builder 的情况下以编程方式创建和使用 UINavigationController.
我希望文档和教程能够向我强调一些事情:
当您创建
UINavigationController
时,您会免费获得一个 UIView 和 UINavigationBar(即您不需要单独添加它们并弄清楚如何将它们连接在一起).您将
myUINavigationController.view
属性添加为主"视图,然后将UIViewController
推送/弹出到 UINavigationController 上,它们将自动显示为在myUINavigationController.view
UIView 中可见.当你将
UIViewController
推送到UINavigationController
上时,UIViewController.navigationController
会被填充.如果你没有推送导航控制器上的视图,我猜该属性是空的.以编程方式将按钮添加到
UINavigationBar
的时间/地点不是您构建UINavigationController
的时间和地点,而是由单个UIViewController
s 当你推送到UINavigationController
时加载它们.我只需要在第一个
viewDidLoad
方法中的UINavigationController
的UINavigationBar
添加一个Done"按钮UIViewController
推送到UINavigationController
.我在第一个视图的 top 上按的任何UIViewController
都会自动在其中有一个后退"按钮,以导航到被掩盖的UIViewController
.p>如果您在将
UIViewController
放入UINavigationController
的堆栈之前设置了title
属性.UINavigationBar
的标题将是您的视图的标题.同样,任何返回"按钮都会自动命名您要返回的视图,而不是一般地说返回".
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:
- (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 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?
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:
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).You add the
myUINavigationController.view
property as the "main" view and then push/popUIViewController
s onto the UINavigationController and they will automatically show up as visible in themyUINavigationController.view
UIView.When you push a
UIViewController
onto aUINavigationController
, theUIViewController.navigationController
is 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
UINavigationBar
is not when and where you construct theUINavigationController
, but rather by the individualUIViewController
s when they are loaded as you push onto theUINavigationController
.I only had to add a "Done" button to the
UINavigationController
'sUINavigationBar
in theviewDidLoad
method of the firstUIViewController
pushed onto theUINavigationController
. AnyUIViewController
I pushed on top of that first view automatically had a "back" button in the to navigate to the covered upUIViewController
.If you set the
title
property of yourUIViewController
before you put it on theUINavigationController
's stack. TheUINavigationBar
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式在 UIViewController 中添加 UINavigationController
基础教程推荐
- 从 UIWebView 访问元数据 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01