【问题标题】:Programmatically add UINavigationController in UIViewController以编程方式在 UIViewController 中添加 UINavigationController
【发布时间】:2011-05-23 00:03:23
【问题描述】:

我目前的视图设置如下:

@interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    UITableView *mainTableView;
}

@property (nonatomic, retain) UITableView *mainTableView;

如您所见,它内部有一个 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];
}

什么都没有发生。出于某种原因,我的UIViewController 内部的UITableView 没有推动视图。这是因为它不是UITableViewController吗?我试着改成那个,还是不行。

我错过了什么吗?

【问题讨论】:

  • BlogViewController 本身是如何显示的?是推送的还是导航控制器的根视图控制器?
  • 这是一个模态视图,使用 presentModalViewController 显示:
  • 如何将 uiviewcontroller 添加到程序中?通过 UINavigationController?

标签: ios uitableview ios4 uinavigationcontroller pushviewcontroller


【解决方案1】:

我发现this blog post 的第一部分对于展示如何在没有 Interface Builder 的情况下以编程方式创建和使用 UINavigationController 很有用。

我希望文档和教程能够向我强调一些事情:

  • 当您创建 UINavigationController 时,您将免费获得一个 UIView 和 UINavigationBar(即您不需要单独添加它们并弄清楚如何将它们连接在一起)。

  • 您将myUINavigationController.view 属性添加为“主”视图,然后将UIViewControllers 推送/弹出到 UINavigationController,它们将自动在myUINavigationController.view UIView 中显示为可见。

  • 1234563 /p>
  • 以编程方式将按钮添加到 UINavigationBar 的时间/地点不是您构建 UINavigationController 的时间和地点,而是由单个 UIViewControllers 在您按下 @ 时加载它们时987654333@。

  • 我只需要在viewDidLoad 方法中将第一个UIViewController 推到UINavigationController 上的UINavigationControllerUINavigationBar 中添加一个“完成”按钮。任何UIViewController 我按在第一个视图的顶部 都会自动在其中有一个“返回”按钮导航到被掩盖的UIViewController

  • 如果您在将UIViewControllertitle 属性放入UINavigationController 的堆栈之前设置它。 UINavigationBars 标题将是您视图的标题。同样,任何“返回”按钮都会自动命名您要返回的视图,而不是一般地说“返回”。

【讨论】:

【解决方案2】:

请访问“How to add UINavigationController Programmatically

请访问上面的链接并获取有关 UINavigationController 的所有信息。

UINavigationController 是 UIViewController 的子类,但与 UIViewController 不同的是,它通常并不意味着您要继承子类。这是因为导航控制器本身很少被定制超出导航栏的视觉效果。 UINavigationController 的实例可以相对容易地在代码或 XIB 文件中创建。

【讨论】:

  • 我认为问题是如何在 UIViewController 中创建 UINavigationController,上面的链接只显示了如何在 AppDelegate 主类中创建 UINavigationController。
猜你喜欢
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2014-11-25
相关资源
最近更新 更多