以编程方式在 UIViewController 中添加 UINavigationController [英] Programmatically add UINavigationController in UIViewController

查看:17
本文介绍了以编程方式在 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 的时间和地点,而是由单个 UIViewControllers 当你推送到 UINavigationController 时加载它们.

  • 我只需要在第一个viewDidLoad方法中的UINavigationControllerUINavigationBar添加一个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/pop UIViewControllers onto the UINavigationController and they will automatically show up as visible in the myUINavigationController.view UIView.

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

  • The time/place to programmatically add buttons to the UINavigationBar is not when and where you construct the UINavigationController, but rather by the individual UIViewControllers when they are loaded as you push onto the UINavigationController.

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

  • If you set the title property of your UIViewController before you put it on the UINavigationController's stack. The UINavigationBars 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆