2016-04-15 13 views
-4

我使用下面的函數用於移動回以前的控制器搬回以前的控制器

- (void) onBack:(id) sender { 
    [[self navigationController] popViewControllerAnimated:NO]; 
} 

它是在正常情況下工作正常,但問題出現了,當我轉到超過1個視圖 - 控制。

如果我從Viewcontroller1移動到ViewController2然後Viewcontroller3

從Viewcontroller3如果我按了後退按鈕,如果我轉到ViewController3來ViewController2,再次回來Viewcontroller2。從ViewController2如果我按下後退按鈕它必須轉到ViewController1,而不是它ViewController3

任何可以幫助我如何解決這個問題。

我使用下面的代碼從一個控制器移動到另一個(只是一個樣品)

[[NSNotificationCenter defaultCenter] addObserverForName:NotificationEditProfile 
    object:nil 
    queue:[NSOperationQueue mainQueue] 
    usingBlock:^(NSNotification *notification) { 
    HUEditProfileViewController *editHUProfileViewController = [[HUEditProfileViewController alloc] initWithNibName:@"EditProfileView" bundle:nil]; 
    editHUProfileViewController.isLocationEnabled = true; 
    [_navigationController pushViewController:editHUProfileViewController animated:NO]; 
               }]; 
+0

如果你想回去1,你可以使用:popToRootViewControllerAnimated任何。 – SeanLintern88

+0

這肯定是不可能的,你必須改變導航堆棧之間的某處,請仔細檢查你的實現。 –

+0

是你的導航控制器嵌入 –

回答

0

我試圖解釋如何導航控制器工作。

它維護導航堆棧。讓我們假設有三個ViewContoller像A,B和C.

最初naviagtion堆棧是空的,所以堆棧頂部是空的。

當您將rootviewController設置爲A時,它將成爲堆棧頂端。屏幕或窗口始終可見。

現在,如果您在A上按B,則B成爲堆棧頂部,因此B在主屏幕上可見。所以現在你的導航堆棧就像A - > B(頂部)。

現在,如果你推C,那麼堆棧就會像這樣A-> B-> C(頂部)。所以,c就在你面前或主屏幕上。

如果現在您彈出視圖控制器,這意味着您從導航堆棧中選擇了removing頂部堆棧。所以如果你調用popViewController,那麼C會從堆棧中移除,所以你的堆棧現在是A-> B(頂部)。

再次按C,然後A-> B-> C(頂部)

再次彈出然後A-> B(頂部)

再次彈出然後A(頂部)。

因此,堆棧的頂部總是可見的,並且較早的vc仍保留在堆棧中。但是當你彈出時,頂部將從堆棧中移除。

所以,如果你從B到C並按回來,然後再次來到B是好的,但是當你從B按回來然後它來到A,因爲你已經從堆棧中刪除C,B是堆棧的頂部,你將要刪除它,所以只有A仍然在堆棧中。

這是導航控制器的流程。

希望你能得到幫助:)

相關問題