简体   繁体   English

如何在AppDelegate中触发navigationController:willShowViewController委托方法

[英]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? 如何在以下实现中触发我的实现的navigationController:willShowViewController委托方法,以便导航控制器中的所有视图控制器都符合colorWithHexString#faf6f5?

Currently, my FirstViewController will be displayed but it doesn't seem to call the delegate method 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). 当前,将显示我的FirstViewController,但它似乎并未调用委托方法来更改其导航栏的颜色(以及随后堆叠在导航控制器上的所有其他视图控制器的颜色)。 Note that I have already added the "UINavigationControllerDelegate" to my app delegate header file. 请注意,我已经将“ UINavigationControllerDelegate”添加到了我的应用程序委托头文件中。

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

}

为什么要尝试在事件方法中而不是在创建UINavigationBar实例时更改tintColor的原因?

Here's how you do it. 这是您的操作方式。 (Note that UIColor doesn't accept hex values; you should use an RGB value, or check this page . (请注意,UIColor不接受十六进制值;您应该使用RGB值,或检查此页面

 - (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 navigationController:willShowViewController:animated:问题 - navigationController:willShowViewController:animated: issue navigationController:willShowViewController:用于ModalViewController吗? - navigationController:willShowViewController: for ModalViewController? 没有进入函数-navigationController:willShowViewController:animated: - Not entering the function -navigationController:willShowViewController:animated: UINavigationController:navigationController:willShowViewController:animated:从未调用 - UINavigationController: navigationController:willShowViewController:animated: never called 如何从AppDelegate向现有的NavigationController添加ViewController? - How to add ViewController to existing NavigationController from AppDelegate? 从Appdelegate触发ViewController中的委托方法 - Triggering delegate method in ViewController from Appdelegate 将一个委托方法多层传递给navigationController堆栈 - Pass a delegate method multi-levels up a navigationController stack 一段时间后如何触发委托方法? - How to trigger a delegate method after a certain period of time? 无法在AppDelegate中初始化委托,但可以在类方法中初始化 - Can't initializing a delegate in the AppDelegate but inside a class method it works 为什么委托不调用NavigationController - Why delegate is not calling on navigationController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM