当我启动应用程序时,下一个视图控制器没有出现

时间:2013-10-03 05:50:54

标签: iphone ios objective-c uiviewcontroller nsuserdefaults

我在运行应用时收到NSLog消息,但nextviewcontroller(欢迎页面)没有出现,我想第一次呼叫nextviewcontroller继续操作。此屏幕只有在我们第一次跑步时才会出现。条件正在运行,但nextviewcontroller未来。我使用此代码

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Configure logging framework

[DDLog addLogger:[DDTTYLogger sharedInstance]];

// Setup the XMPP stream

[self setupStream];
// Override point for customization after application launch.
BOOL launch = [[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchingFirstTime"];

if(!launch){
    NSLog(@"first");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LaunchingFirstTime"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.viewController1 = [[welcomePage alloc] initWithNibName:@"welcomePage" bundle:nil];
    self.window.rootViewController = self.viewController1;
}
else{

    NSLog(@"second");
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;


}

[self.window makeKeyAndVisible];
if (![self connect])
{
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //messageTableViewController
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        //[navigationController presentModalViewController:settingsViewController animated:YES];
    });
}
return YES;

4 个答案:

答案 0 :(得分:0)

如果您没有使用故事板,并且您的窗口不是IBOutlet,请执行此操作。

 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 //add this
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 self.window.backgroundColor=[UIColor clearColor];

BOOL launch = [[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchingFirstTime"];
if(!launch){
    NSLog(@"first");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LaunchingFirstTime"];
    self.viewController1 = [[welcomePage alloc] initWithNibName:@"welcomePage" bundle:nil];
    self.window.rootViewController = self.viewController1;
}
else{

    NSLog(@"second");
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;


}

答案 1 :(得分:0)

如果您使用的是UIStoryBoard,请尝试使用此

`instantiateViewControllerWithIdentifier` .: application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {    
         BOOL launch = [[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchingFirstTime"];
                if(!launch){
                    NSLog(@"first");
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LaunchingFirstTime"];
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController_ID"];

                    self.window.rootViewController = self.viewController1;
                }
                else{

                    NSLog(@"second");
                    self.viewController = // Follow the above
                    self.window.rootViewController = self.viewController;    
                }

如果您不使用故事板,请简要描述您的问题,<。p>

答案 2 :(得分:0)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    BOOL launch = [[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchingFirstTime"];
    if(!launch){
        NSLog(@"first");
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LaunchingFirstTime"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.viewController1 = [[welcomePage alloc] initWithNibName:@"welcomePage" bundle:nil];
        self.window.rootViewController = self.viewController1;
    } else {
        NSLog(@"second");
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;    
    }
}

答案 3 :(得分:0)

使用synchronize方法立即将用户默认值写入磁盘。

[[NSUserDefaults standardUserDefaults] synchronize];

来自Apple Docs:

synchronize method writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

您的代码中缺少它。将其写在if子句中。