2014-12-23 173 views
0

我有兩個視圖控制器,一個被稱爲Main,另一個被稱爲InserirCodigoViewController。我有一個叫做'contarParaMudar'的int。我的代碼是:Xcode 6更改視圖控制器

-(IBAction)ok:(id)sender { 
    if (completeLevel == true) { 

     contarParaMudar = 1; 

    } else if (contarParaMudar == 1) { 

     UIStoryboard *mainStoryboar = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
     UIViewController *vc = [mainStoryboar instantiateViewControllerWithIdentifier:@"InserirCodigoViewController"]; 
     [self presentViewController:vc animated:YES completion:nil]; 

    } 
} 

但我想以下幾點:當用戶打開應用程序和completeLevel = true,在iphone必須直接加載InserirCodigoViewController

我能做些什麼? (對不起,我的英語不好)。

+0

這能解決你的問題嗎? –

回答

0

也只是在MainVC添加viewDidAppear方法,並執行您發佈它裏面的代碼...

要存儲BOOL NSUserDefaults的使用

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"completeLevel"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

加載

if([[NSUserDefaults standardUserDefaults] boolForKey:@"completeLevel"] == YES) { 
    //load your VC 
} 
+0

我有這個錯誤:https://www.youtube.com/watch?v=gm5tbviUjpA&feature=youtu.be –

+0

Hmm viewDidAppear每次出現視圖時都會調用...也許你關閉了這個視圖,completeLevel仍然是正確的? –

+0

您能通過電子郵件向我發送您的源代碼嗎?我認爲這將是解決問題的最佳方式 –

0

我在做我的項目類似的東西,我保存completeLevel到NSUserDefaults,然後根據它的值決定視圖控制器sh流在我的appdelegate

或者你可以使用viewDidAppear通過新

0

套裝completeLevel作爲建議YES當一個級別完成,首次使NO。您可以在NSUserDefaults存儲BOOL值是這樣的:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"completeLevel"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

As you said "when the user opens the app and completeLevel = true , the iphone must load directly InserirCodigoViewController"

對於這個在你的AppDelegate的didFinishLaunchingWithOptions,你必須這樣做:

BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:@"completeLevel"] == YES; 
NSString *storyboardId = isLoggedIn ? @"Main" : @"InserirCodigoViewController"; 
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId]; 

希望這有助於... :)