在应用程序运行时,在AppDelegate中运行didFinshLaunchingWithOptions?

时间:2015-02-20 14:27:04

标签: ios objective-c xcode xcode6 appdelegate

有没有办法可以执行

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
应用程序从另一个ViewController运行时的

方法?

2 个答案:

答案 0 :(得分:1)

在您的app委托中维护一个单独的方法,以设置导航栏外观。

在AppDelegate.h文件中,声明相同的方法。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

-(void)setNavBarAppearance;

@end

在appDelegate.m文件中,编写该方法的功能

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self setNavBarAppearance];
}


-(void)setNavBarAppearance {
    //Do what is required here.
}

@end

然后,无论你需要调用相同的方法:

#import "AppDelegate.h"

AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate setNavBarAppearance];

答案 1 :(得分:0)

您不应该自己调用委托方法,可以使用NSNotificationCenter

  1. 在我们的案例中,以不同的方法(someThingInterestingHappened)编写要执行的代码。
  2. addObserver上致电defaultNotificationCenter,即[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someThingInterestingHappened:) name:@"desiredEventHappend" object:nil];
  3. ,将课程注册为通知
  4. 发布任何课程[[NSNotificationCenter defaultCenter] postNotificationName:@"desiredEventHappend" object:nil];
  5. 的通知