Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 启动应用程序时,下一个视图控制器未出现_Iphone_Ios_Objective C_Uiviewcontroller_Nsuserdefaults - Fatal编程技术网

Iphone 启动应用程序时,下一个视图控制器未出现

Iphone 启动应用程序时,下一个视图控制器未出现,iphone,ios,objective-c,uiviewcontroller,nsuserdefaults,Iphone,Ios,Objective C,Uiviewcontroller,Nsuserdefaults,当我运行应用程序时收到NSLog消息,但nextviewcontroller(欢迎页)未出现,我想第一次调用nextviewcontroller,继续操作。只有在我们第一次运行时,此屏幕才会第一次出现。条件正在工作,但下一个视图控制器不来。我使用此代码是为了 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.win

当我运行应用程序时收到
NSLog
消息,但
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;

如果您不使用故事板,并且您的窗口不是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;


}

如果您使用的是
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;    
                }

如果您没有使用故事板,请简要描述您的问题。

使用
同步
方法使用户默认值立即写入磁盘

- (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;    
    }
}
[[NSUserDefaults standardUserDefaults] synchronize];
来自苹果文档:

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

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

你的窗口在哪里?它是一个出口还是你正在使用故事板?每次你以“第一个”的形式获得NSLog时?我猜[self-connect]第一次返回false,你的调度在代码运行后,因此你看不到欢迎页面。@我需要做什么来解决这个问题[self-connect]里面有什么逻辑?改变它。您不需要第一次运行if块。self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];我的代码中有这一行,后面也没有,但在输出窗口中,我首先收到TradeSender[65134:c07]这条消息,当我在运行而不是屏幕时,你能给你的视图控制器设置一些背景色并测试你是否看到了什么吗?是的,在示例项目中使用了相同的代码,但它不起作用是你的视图控制器好吗??我的意思是,它是否所有的插座都正确接线?我没有使用storyborad,我在第一个和第二个控制器上都使用了xib。我想我的条件正在工作,因为当我第一次运行时,nslog消息会出现在输出窗口中,但屏幕第一个(wecome页面)没有出现。您尝试过调试吗?这将告诉您应用程序所采用的确切流程。如果(!launch){[[NSUserDefaults standardUserDefaults]synchronize];则在设置LaunchingFirstTime键后,第二个屏幕出现first timeWrite synchronize方法。此外,请尝试调试并发布完整的didFinishLaunchingWithOptions方法。