关于initWithNavigationBarClass的困惑 - 如何使用(new instanceType方法) [英] Confusion about initWithNavigationBarClass - how to use (new instanceType method)

查看:718
本文介绍了关于initWithNavigationBarClass的困惑 - 如何使用(new instanceType方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很好用:

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

工作:

but this does not work:

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

怎么会这样?谢谢

推荐答案

self.window.rootViewController.viewControllers = @[firstPage];

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

does not compile because the rootViewController property of UIWindow is declared as a (generic) UIViewController (which does not have a viewControllers property), and not as a UINavigationController.

编译器没有知道你的情况下,根视图控制器实际上是一个导航
控制器。

The compiler does not "know" that the root view controller is actually a navigation controller in your case.

所以要么你在第一个代码块中继续,要么你有添加显式强制转换:

So either you proceed as in your first code block, or you have to add an explicit cast:

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

这篇关于关于initWithNavigationBarClass的困惑 - 如何使用(new instanceType方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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