Confusion about initWithNavigationBarClass - how to use (new instanceType method)

試著忘記壹切 提交于 2019-12-19 19:13:29

问题


This works great:

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

how can it be? Thanks


回答1:


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

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];


来源:https://stackoverflow.com/questions/22286166/confusion-about-initwithnavigationbarclass-how-to-use-new-instancetype-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!