1

我有一个与用户“Flocked”类似的问题(请参阅此处的问题:Load an other View from applicationDidFinishLaunching in the AppDelegate),但是在阅读了他的帖子后我没有设法解决,也许我的情况与他的不同。

我有didFinishLaunchingWithOptions一个例程来检查应用程序是否是第一次运行(也来自另一个用户):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {

        NSLog(@"First run");
    }

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

如果 firstLaunch 发生,我无法加载另一个视图InfoView(用于设置的视图)。我试过了:

InfoView *infoView = [[InfoView alloc]init];
[self presentViewController:infoView animated:YES completion:nil];

里面if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])

但没有运气。有什么想法或帮助吗?

4

2 回答 2

2

试试这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    if (![defaults boolForKey:@"everLaunched"]) {
        NSLog(@"First run");
        [defaults setBool:YES forKey:@"everLaunched"];
        InfoView *infoView = [[InfoView alloc]init];
        [self.viewController presentViewController:infoView animated:YES completion:nil];    
    }

    return YES;
}
于 2012-11-23T16:00:10.507 回答
0

您正在尝试调用[self presentViewController:infoView animated:YES completion:nil];self这是一流的,UIResponder因为您在您的AppDelegate. 您应该在 UIViewController 上调用它,在您的情况下可能是viewController

编辑:这是要调用的行,调用 [self.window makeKeyAndVisible]; otherwise self.window.rootViewController后将为零。

[self.window.rootViewController presentViewController:infoView animated:YES completion:nil];
于 2012-11-23T15:56:43.207 回答