Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popping to a specific viewcontroller in a navigation stack

I have a come across a piece of code to pop to a specific viewcontroller in a navigation stack as below

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

The objective is to pop to MyGroupViewController. But I am not understanding this line of code.

MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;

Whats exactly happening here. I don't think a new instance of MyGroupViewController is being created here.

like image 281
Jean Paul Scott Avatar asked Jun 14 '12 04:06

Jean Paul Scott


People also ask

How do I pop to a specific view in Swiftui?

ContentView -> View1 -> View2 And from View2 you want to pop to the Root view.

How do I pop two viewControllers at a time in Swift?

How do I pop two viewControllers at a time in Swift? There's also a setViewControllers(_:animated:) to include the pop animation. Alternatively you could find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.


3 Answers

//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];
    }
}

Also you can do like this

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

Swift code

//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)
  }
}
like image 131
Inder Kumar Rathore Avatar answered Oct 06 '22 01:10

Inder Kumar Rathore


The view controllers of a navigation controller stack are being enumerated. Since these view controllers can be of any kind (but will always inherit from UIViewController), the generic UIViewController is used. However, the compiler will not know what type that view controller is, so it is being casted to a MyGroupViewController. When that happens, the compiler knows what the type of class and you can send it messages that only apply to that class.

In this case it is kind of unnecessary, as it could be simplified to this:

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

Short answer: it changes a variable type to the type specified in the parentheses to avoid compiler warnings.

like image 36
Scott Berrevoets Avatar answered Oct 06 '22 00:10

Scott Berrevoets


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];   
  }   
} 
like image 25
ganesh manoj Avatar answered Oct 06 '22 00:10

ganesh manoj