从AppDelegate.m加载视图(默认情况下的其他视图)

时间:2012-11-23 15:42:44

标签: ios xcode uiview

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

我的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"])

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

您正试图在[self presentViewController:infoView animated:YES completion:nil];上致电self UIResponder,因为您在AppDelegate[self.window makeKeyAndVisible];。您应该在UIViewController上调用它,可能在您的情况下 viewController

编辑:这是要拨打的电话, otherwise self.window.rootViewController [self.window.rootViewController presentViewController:infoView animated:YES completion:nil]; 后,将为

{{1}}