2013-06-12 12 views
7

この資料を以下でどのように達成するには?私にそれのためのいくつかの指導を与えてください。私は以下の問題を説明します。iOS:アプリをクラッシュして再起動した後、以前のviewControllerを開くことは可能ですか?

私はホームボタンをタップしてトレイからアプリを削除すると、アプリを開いているときにログイン画面が表示されます。私はNSUserDefaultsの使い方を知っています。

私の問題は、3番目または4番目に移動したときにviewControllerとホームボタンを押してトレイからアプリケーションを削除したときです。最後に開こうとするよりもアプリを開く度に開いていますviewController

また、私のアプリがクラッシュしているときに同じですが、もう一度開いていて、最後に開いたviewController状態のアプリを開きたいとします。

だから私はそれが可能かどうかを知りたいのですか?はいの場合は、この物を達成する方法を教えてください。

は私がちょうどナビゲーションアレイ上の一番上のオブジェクトのnsuserdefault値を設定し、すべてのビューコントローラのviewDidLoadに私の提案

を与えるので、あなたは、コードの一部でOKです理解して、あなたに

答えて

2

ありがとうございます。

あまりにも多くの枝が自分でないならば、あなたは簡単に

+0

おかげで、あなたのアイデアが良いですが、私は心配月どのようにnavigaiton +タブバーでこのすべての私のアプリを管理する方法とログイン管理と使用の2つのタイプ..?どのようにLoginScreenとそれに関連するセッションとユーザーを管理するか? –

+0

私はあなたがクラスを作成しなければならないと思う(シリアル化されている)、それからアカウントの種類、タブのインデックス、viewcontrollerのようなすべての必要な情報を保存します。 nsdata – amar

3

はいルートビューコントローラでプッシュを管理することができ、両方のケースが考えられます。

クラッシュ時にを使用すると、UncaughtExceptionHandlerを使用してコードを実行できます。あなたのアプリデリゲートでは、あなたがこのようにハンドラ・登録:

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

NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 
// Other didFinishLaunchingWithOptions code 

を、アプリが最後に選択したタブバーを追跡するために、を実行している間、同じ.mファイル

void uncaughtExceptionHandler(NSException *exception) 
{ 
    // App crashed, save last selected tabbar index to the to the NSUserDefaults 
    [[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"LastSelectedTabbarIndex"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

にあなたのハンドラメソッドを追加コントローラの場合はUITabBarControllerDelegateを使用し、新しく選択したタブバーのインデックスをNSUserDefaultsに保存します。ショート例:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    NSUInteger tabIndex = [[tabBarController viewControllers] indexOfObject:viewController]; 

    // I have newly selected index, now save it to the NSUserDefaults 
} 

このコードは、NSUserDefaultsたびにタブバーの選択率の変化に最後に選択されたタブバーのインデックスを保存します。

最後に

、あなたの最後の読みアプリ開始(あなたのdidFinishLaunchingWithOptionsでは)、それに応じ

self.tabBarController.selectedIndex = lastSelectedIndexFromDefaults; 

編集NSUserDefaultsからタブバーインデックスを保存し、タブバーの選択されたインデックスを設定します。あなたもUINavigationControllerのコントローラを復元する必要がある場合は をスタックは、かなり困難な作業です。私はあなたに私の頭に何が来るのかを簡単に概観します。

2例があります。

  • カスタムビューコントローラの初期化子を持っており、それらのコントローラにカスタムオブジェクトを渡す必要があります - (いくつかの合理的な時間内)。この場合、そのほとんど不可能この
  • を実装
  • ナビゲーションスタック内のView Controllerを初期化するには、-initまたは-initWithNibName...:のみを使用します。タブのルートUINavigationControllerからコントローラを列挙し、そのクラス名をNSStringFromClassで取得し、NSUserDefaultsに保存します。アプリケーションの起動時には、手順を逆にします(UIViewController *vc = [[NSClassFromString(@"aa") alloc] init];などを使用して、NSUserDefaultsから読み取られた名前の文字列を使用してコントローラを初期化します)。
+0

としてユーザーのデフォルトで保存しますが、Tabbarviewcontroller - > anotherVIewcontroller ..のナビゲートはどうですか? –

+0

情報のためのthxしかし、あなたのアイデアは良いですが、私はnavigaiton + tabbarとこれもすべての私のアプリを管理する方法と、ログイン管理と使用の2つのタイプを心配?..?どのようにLoginScreenとそれに関連するセッションとユーザーを管理するか? –

+0

申し訳ありませんが、私はもはや詳細をあなたを助けることができません: - /可能な解決策はあなたのアプリのアーキテクチャに強く依存します、私はあなたがそれを理解してくれることを願っています: –

2

これは適切な答えではありませんが、起動後にビューをナビゲートするために使用できます。コード下のAppDelegateファイルの使用では

:---

#import "NewSAppDelegate.h" 
#import "NewSViewController.h" 

static NewSAppDelegate *globalSelf; 

@implementation NewSAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[NewSViewController alloc] initWithNibName:@"NewSViewController" bundle:nil]; 
    self.navController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = self.navController; 
    [self.window makeKeyAndVisible]; 
    globalSelf=self; 
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 

    return YES; 
} 

void uncaughtExceptionHandler(NSException *exception) 
{ 
    UIViewController *currentVC = globalSelf.navController.visibleViewController; 
    [[NSUserDefaults standardUserDefaults] setObject:NSStringFromClass(currentVC.class) forKey:@"lastVC"]; 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    UIViewController *currentVC = self.navController.visibleViewController; 
    [[NSUserDefaults standardUserDefaults] setObject:NSStringFromClass(currentVC.class) forKey:@"lastVC"]; 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil]; 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

ログインでのViewControllerのinitメソッドは、通知のためのオブザーバーを追加して、通知方法では、あなたがいる場合にのViewControllerの名前received.andプッシュのための条件を適用することができます起動LoginViewコントローラ上のもののViewControllerのように:---

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(openLastVC) 
                name:@"appDidBecomeActive" 
                object:nil]; 

     // Custom initialization 
    } 
    return self; 
} 

-(void)openLastVC 
{ 
    NSLog(@"val ==%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"lastVC"]); 

    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"lastVC"] isEqualToString:@"GhachakViewController"]) { 
     GhachakViewController *gvc=[[GhachakViewController alloc] initWithNibName:@"GhachakViewController" bundle:nil]; 
     [self.navigationController pushViewController:gvc animated:NO]; 
    } 
} 

は、このヘルプはあなた....返信用

関連する問題