【问题标题】:Confusion about initWithNavigationBarClass - how to use (new instanceType method)关于 initWithNavigationBarClass 的困惑 - 如何使用(新的 instanceType 方法)
【发布时间】:2014-03-09 18:21:15
【问题描述】:

这很好用:

UINavigationController *nc =
    [[UINavigationController alloc]
    initWithNavigationBarClass:[GTScrollNavigationBar class]
    toolbarClass:[UIToolbar class]];
nc.viewControllers = @[firstPage];
self.window.rootViewController = nc;

但这确实有效:

UINavigationController *nc =
    [[UINavigationController alloc]
    initWithNavigationBarClass:[GTScrollNavigationBar class]
    toolbarClass:[UIToolbar class]];
self.window.rootViewController = nc;
self.window.rootViewController.viewControllers = @[firstPage]; // ERROR

怎么可能?谢谢

【问题讨论】:

    标签: ios uinavigationcontroller instancetype


    【解决方案1】:
    self.window.rootViewController.viewControllers = @[firstPage];
    

    因为声明了UIWindowrootViewController 属性而无法编译 作为(通用)UIViewController(没有viewControllers 属性),而不是UINavigationController

    编译器并不“知道”根视图控制器实际上是一个导航 控制器在你的情况下。

    因此,要么像在第一个代码块中那样继续,要么必须添加显式转换:

    ((UINavigationController *)self.window.rootViewController).viewControllers = @[firstPage];
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 1970-01-01
      • 2015-04-28
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 2013-08-11
      相关资源
      最近更新 更多