Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 272
Fattie Avatar asked Nov 01 '22 03:11

Fattie


1 Answers

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];
like image 173
Martin R Avatar answered Nov 13 '22 16:11

Martin R