开发者

How to trigger navigationController:willShowViewController delegate method in AppDelegate

How can I trigger the navigationController:willShowViewController delegate method for my implementation below so that all the view controllers in the navigation controller will conform to the colorWithHexString #faf6f5?

Currently, my FirstViewController will be displayed but it doesn't seem to call the delegate meth开发者_Go百科od to change the color of it's navigation bar (as well as for all other view controllers that are stacked onto the navigation controller subsequently). Note that I have already added the "UINavigationControllerDelegate" to my app delegate header file.

//In App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Set First View
    FirstViewController *firstView = [[FirstViewController alloc]init];

    // pushes a nav con 
    UINavigationController *tempNavcon = [[UINavigationController alloc]initWithRootViewController:firstView];
    self.navcon = tempNavcon;

    [self.window addSubview:navcon.view];

}

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#faf6f5"];

}


is there a reason why you are trying to change the tintColor in an event method rather than when the UINavigationBar instance is created?


Here's how you do it. (Note that UIColor doesn't accept hex values; you should use an RGB value, or check this page.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Initialize your view controller.
    FirstViewController * firstView = [[FirstViewController alloc] init];

    // Create an instance of a UINavigationController. Its stack contains only firstView.
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:firstView];

    //Here is where you set the color of the navigationBar. See my note above for using RGB.
    navController.navigationBar.tintColor = [UIColor greenColor];

    // You can now release the firstView here, navController will retain it
    [firstView release];

    // Place navigation controller's view in the window hierarchy
    [[self window] setRootViewController:navController];

    [navController release];

    [self.window makeKeyAndVisible];
    return YES;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜