2015-11-06 105 views
0

我是這個領域的新手。我正在開發一個應用程序,用戶可以從應用程序中的任何頁面註銷。從iOS註銷App

我正在使用此方法進行註銷過程。 (從What is the perfect way to make a logout from IOS app?簡稱)

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
     if (buttonIndex == 0) { 
      NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 
      [defaults setObject:nil forKey:@"UserId"]; 
      [defaults synchronize]; 
      //redirect to login view 

      NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate]; 
      LoginViewController *second = [[LoginViewController alloc]initWithNibName:nil bundle:nil]; 
      [appsDelegate.window setRootViewController:nil]; 
      [appsDelegate.window setRootViewController:login]; 
    } 
} 

我的問題是如何進行註銷之前關閉所有打開的ViewController?當我執行上面的方法時,我點擊了註銷按鈕的頁面在後臺保持打開狀態。任何人都可以幫助我。提前致謝。

+0

註銷成功後,您應該執行「關閉」堆棧中的所有VC。只需調用'[self.navigationController popToRootViewControllerAnimated:YES];'。這將彈出導航堆棧中所有現有的VC並將您帶回根目錄Vc – NSNoob

+0

viewcontroller停留在後臺?你是什​​麼意思。它仍然可見嗎? – MarkHim

+0

謝謝@ NSNoob .. Bt這個應用程序不使用導航控制器。有沒有其他的方式.. ?? – luckyShubhra

回答

1

要首先解散所有模態UIViewControllers,您可以做某事。像這種方法

-(void)dismissModalStack { 
    UIViewController *vc = self.presentingViewController; 
    while (vc.presentingViewController) { 
     vc = vc.presentingViewController; 
    } 
    [vc dismissViewControllerAnimated:YES completion:NULL]; 
} 

(如這裏看到:iPhone - dismiss multiple ViewControllers

,而不是

[self.navigationController popToRootViewControllerAnimated:YES]; 

,因爲這只是彈出您的推UIViewControllers你的導航堆棧。因此,對於你

這將是

-(void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex { 
    if (buttonIndex == 0) { 
     NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 
     [defaults setObject: nil forKey: @"UserId"]; 
     [defaults synchronize]; 

     UIViewController *vc = self.presentingViewController; 
     while (vc.presentingViewController) { 
      vc = vc.presentingViewController; 
     } 
     [vc dismissViewControllerAnimated:YES completion:NULL]; 

     NewClassMoonAppDelegate * appsDelegate = [[UIApplication sharedApplication] delegate]; 
     LoginViewController * second = [[LoginViewController alloc] initWithNibName: nil bundle: nil]; 
     [appsDelegate.window setRootViewController: login]; 
    } 
} 
+0

是的..它的工作..但現在的問題是,LoginViewController頁面仍然在後臺打開。我應該檢查LoginViewController並不關閉它在dismissModal堆棧.. ?? – luckyShubhra

+0

難道你不想讓loginViewController可見嗎?你想實現的目標狀態是什麼? – MarkHim

+0

我希望loginViewController可見。 Bt我得到2個loginViewController一個在前臺以及一個在後臺。 – luckyShubhra

1

這是一個NSNotification一個完美的工作:當用戶點擊一個退出按鈕,你開除一個自定義通知,像這樣:

[[NSNotificationCenter defaultCenter] postNotificationName:@"userWillLogoutNotification" object:nil userInfo:nil]; 

然後每個視圖/導航/選項卡/任何控制器都可以相應地作出反應並「重置」它自己。

執行此導航任務不是「註銷」按鈕的任務,每個控制器只處理其自己的業務,並對此類應用程序範圍內的通知作出反應。

+0

M新的這個領域..我可以知道我如何訪問所有頁面中的通知...? – luckyShubhra

+1

每個控制器註冊自己到默認的NSNotificationCenter,它向所有的監聽器廣播通知。對於這種事件來說,這是一種常見的方法(將一個「信號」發送給多個潛在未知的聽衆) – Cyrille