Sergey Grishchev
Sergey Grishchev

Reputation: 12051

Checking if a ViewController on Navigation Stack exists

I have this code here to check on the existence of the ViewController. And unfortunately it doesn't work. The thing is, it is executed at the moment of another VC popping from the Navigation Stack:

- (void) leftViewDidHide{
    if ([((AppDelegate *)[UIApplication sharedApplication].delegate).frontViewController.navigationController.viewControllers objectAtIndex:1]) {
    SGServerListViewController *sample = [[[((AppDelegate *)[UIApplication sharedApplication].delegate).frontViewController.navigationController.viewControllers objectAtIndex:1]childViewControllers] objectAtIndex:0];
    [sample.serverTableView setUserInteractionEnabled:YES];
    }
}

The app crashes with an exception breakpoint pointing me to the line with an if statement. Any ideas on what could be wrong here? I'm just trying to check if this VC is there and if it is - execute the code.

Upvotes: 8

Views: 14937

Answers (6)

Paresh Prajapati
Paresh Prajapati

Reputation: 11

You can do that (checking if a ViewController on Navigation Stack exists) with this code:

if navigationController?.viewControllers.count > 1 {
    for root in (self.navigationController?.viewControllers)! {
        if root is ViewController {
           let i = root as? ViewController
            i?.table.removeFromSuperview()
        }
    }
}
self.navigationController?.popToRootViewControllerAnimated(true)

Upvotes: 1

AlQaim Solutions
AlQaim Solutions

Reputation: 69

NSArray *viewControlles = self.navigationController.viewControllers;

for (int i = 0 ; i <viewControlles.count; i++){
    if ([[viewControlles objectAtIndex:i] isKindOfClass:[RequiredViewController class]]) {

        //Enter your code

    }
}

Upvotes: 0

SAE
SAE

Reputation: 1739

- (void) leftViewDidHide{

if ([((AppDelegate *)[UIApplication sharedApplication].delegate).frontViewController.navigationController.viewControllers count] == 1) {

         SGServerListViewController *sample = [[[((AppDelegate *)[UIApplication sharedApplication].delegate).frontViewController.navigationController.viewControllers objectAtIndex:1]childViewControllers] objectAtIndex:0];
         [sample.serverTableView setUserInteractionEnabled:YES];

     }
 }

Upvotes: 2

iosCurator
iosCurator

Reputation: 4466

-(BOOL)isControllerAlreadyOnNavigationControllerStack{

    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:Controller.class]) {
            [self.navigationController popToViewController:vc animated:NO];
            return YES;
        }
    }
    return NO;
}


if (![self isControllerAlreadyOnNavigationControllerStack]) {
    //push controller 
}

Upvotes: 3

Durga Vundavalli
Durga Vundavalli

Reputation: 1828

 NSArray *controllerArray = [self.navigationController.viewControllers];
//will get all the controllers added to UINavigationController.

    for (id controller in controllerArray)
   { 
      // iterate through the array and check for your controller
      if ([controller isKindOfClass:[checkYourController class]]) 
          {
              //do your stuff here
          }
    }

just for an idea containsObject: method of NSArray class might also work.

Upvotes: 5

Dipak_IOS
Dipak_IOS

Reputation: 156

NSArray *viewControlles = [self.navigationController.viewControllers];

for (int i = 0 ; i <viewControlles.count; i++){ 
  if ([YourVC isKindOfClass:[viewControlles objectAtIndex:i]]) {
          //Execute your code
      }
}

Upvotes: 7

Related Questions