2016-09-13 17 views
0

我是iOS開發新手。我的問題是,我有兩個視圖控制器。如何在iOS中管理狀態目標c

的viewController - 一個的viewController - B

現在,如果我殺了從的viewController應用程序 - 一個比重新啓動應用程序。比應用程序必須打開viewController - 答,如果我殺了從viewController - B的應用程序,並重新啓動應用程序。比應用程序必須打開viewController - B.

任何人都可以幫助我,我已經完成了RND,但找不到合適的解決方案。

感謝

+0

開始通過閱讀:https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html –

+0

也有一個教程在這裏:https://www.raywenderlich.com/117471/ state-restoration-tutorial –

+0

(本教程在Swift中,但Objective-C中使用的類和方法應該相同)。 –

回答

0

applicationWillTerminate可以使用,但它只會被調用,如果用戶退出應用程序從forground如果您的應用程序是在後臺,然後用戶退出應用程序,然後applicationWillTerminate將不會被調用。

所以,你必須照顧applicationDidEnterBackground

所以,當應用程序在後臺輸入(即調用applicationDidEnterBackground)或致電applicationWillTerminateuser defaults保存狀態(當前的VC)。

現在在你的didFinishLaunchingWithOptions設置該視圖控制器爲rootviewcontroller或任何你想要管理它的方式。

參考:Apple documentation for applicationWillTerminate

PS:你不應該管理的應用程序是這樣的。這是可怕的方式!如果可能的話,以正常流程運行你的應用程序在AppDelegate.m

1
  1. 化妝sharedDelegate文件

    +(AppDelegate中*)sharedDelegate {

    回報(AppDelegate中*)[UIApplication的sharedApplication] .delegate;

    }

  2. appdelgate.h

    +(AppDelegate中*)sharedDelegate; @property(nonatomic,strong)NSString * currentViewContoller;

  3. 當推到anyContoller然後設置Appdelegates currentViewContoller到新的VC

的viewController * VC = [[ALLOC的viewController] INIT]; [self.navigationController pushViewController:vc animated:YES];

[AppDelegate sharedDelegate]。currentViewContoller = NSStringFromClass([viewController class]);

  • 現在,當應用程序終止

    • (無效)applicationWillTerminate:(UIApplication的*)應用{

    //當應用程序要調用終止。如果適用,保存數據。另請參閱applicationDidEnterBackground :.

  • [[NSUserDefaults的standardUserDefaults]的setObject:[AppDelegate中sharedDelegate] .currentViewContoller forKey:@ 「cuurentVC」];

    } 
    

    5現在,當應用程序啓動第一次檢查前一個控制器,當應用程序終止

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
    
        NSString *string=[[NSUserDefaults standardUserDefaults] valueForKey:@"cuurentVC"]; 
    

    ,推動該類

    UIViewController *object = [[NSClassFromString(string) alloc] init...]; 
    
    }