2012-06-14 52 views
5

我有一個遇到的一段代碼彈出到特定的ViewController在導航堆棧如下膨化到特定的ViewController在導航堆棧

for (UIViewController* viewController in self.navigationController.viewControllers) { 
    if ([viewController isKindOfClass:[MyGroupViewController class]]) { 
     MyGroupViewController *groupViewController = (MyGroupViewController*)viewController; 
     [self.navigationController popToViewController:groupViewController animated:YES]; 
    } 
} 

目的是彈出到MyGroupViewController。但我不理解這行代碼。

MyGroupViewController *groupViewController = (MyGroupViewController*)viewController; 

這裏到底發生了什麼。我不認爲在這裏創建了MyGroupViewController的新實例。

回答

21
//This for loop iterates through all the view controllers in navigation stack. 
for (UIViewController* viewController in self.navigationController.viewControllers) { 

    //This if condition checks whether the viewController's class is MyGroupViewController 
    // if true that means its the MyGroupViewController (which has been pushed at some point) 
    if ([viewController isKindOfClass:[MyGroupViewController class]]) { 

     // Here viewController is a reference of UIViewController base class of MyGroupViewController 
     // but viewController holds MyGroupViewController object so we can type cast it here 
     MyGroupViewController *groupViewController = (MyGroupViewController*)viewController; 
     [self.navigationController popToViewController:groupViewController animated:YES]; 
    } 
} 

你也可以像這樣

if ([viewController isKindOfClass:[MyGroupViewController class]]) { 
      [self.navigationController popToViewController:viewController animated:YES]; 
} 

斯威夫特代碼

//Itrate through all the view controllers in navigation stack 
for vc in self.navigationController!.viewControllers { 
    // Check if the view controller is of MyGroupViewController type 
    if let myGropupVC = vc as? MyGroupViewController { 
    self.navigationController?.popToViewController(myGropupVC, animated: true) 
    } 
} 
+0

「這裏viewController是MyGroupViewController的基類UWiewController的引用 ,但viewController保存MyGroupViewController對象,因此我們可以在此處鍵入轉換它」。這正是我正在尋找的。因此,使用了已經在導航堆棧中的同一個MyGroupViewController實例。對? –

+0

是的,它與之前推送的實例相同。 –

2

正在枚舉導航控制器堆棧的視圖控制器。由於這些視圖控制器可以是任何類型的(但總是從UIViewController繼承),所以使用了通用的UIViewController。但是,編譯器不會知道該視圖控制器是什麼類型,因此它將被轉換爲MyGroupViewController。當發生這種情況時,編譯器知道類的類型,並且可以發送只適用於該類的消息。

在這種情況下,它是一種不需要的,因爲它可以簡化爲這樣:

(UIViewController* viewController in self.navigationController.viewControllers) { 
    if ([viewController isKindOfClass:[MyGroupViewController class]]) { 
     [self.navigationController popToViewController:viewController animated:YES]; 
    } 
} 

簡短的回答:它改變一個變量類型在括號中指定,以避免編譯器警告類型。

+0

我沒有這個視圖 - 控制實例在我目前的類。 –

+0

@jean Paul Scott你已經在快速枚舉中創建了UIViewController的一個實例,或者你可以說foreach循環 –

+0

'MyGroupViewController * groupViewController =(MyGroupViewController *)viewController;'我想知道在這段代碼中會發生什麼。這個groupViewController是導航堆棧中的同一個實例嗎? –

1
for (int m=0; m<[self.navigationController.viewControllers count]; m++) 
{ 

     if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[MyGroupViewController class]]) 
      { 

       MyGroupViewController * groupViewController = [self.navigationController.viewControllers objectAtIndex:m]; 
       [self.navigationController popToViewController:groupViewController animated:YES]; 
      } 
} 
+1

我不認爲你回答我的問題。 –

+2

該行將提供所需的視圖控制器的引用 –

0
- (void) RetunToSpecificViewController{ 

    for (UIViewController *controller in self.navigationController.viewControllers) { 
     if ([controller isKindOfClass:[AnOldViewController class]]) { 
     //Do not forget to import AnOldViewController.h 

      [self.navigationController popToViewController:controller 
               animated:YES]; 
      break; 
     } 
    } 

斯威夫特

func RetunToSpecificViewController() 
{ 
    let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController] 
    self.navigationController!.popToViewController(viewControllers[viewControllers.count 
     - 5], animated: true) 
} 

我們寫一個更好的教程,你可以檢查 https://appengineer.in/2014/03/13/pop-to-specific-view-controller-in-ios/