Popping to a specific viewcontroller in a navigation stack

后端 未结 4 1603
Happy的楠姐
Happy的楠姐 2021-02-10 07:47

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.navigationC         


        
相关标签:
4条回答
  • 2021-02-10 07:52
    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];   
                 }   
    } 
    
    0 讨论(0)
  • 2021-02-10 07:54
    //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)
      }
    }
    
    0 讨论(0)
  • 2021-02-10 07:55
    - (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;
            }
        }
    

    On Swift

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

    We have write a better tutorial on that , You can check https://appengineer.in/2014/03/13/pop-to-specific-view-controller-in-ios/

    0 讨论(0)
  • 2021-02-10 08:01

    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.

    0 讨论(0)
提交回复
热议问题