7

我在这里有这段代码来检查ViewController. 不幸的是,它不起作用。问题是,它是另一个 VC 从以下位置弹出时执行的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];
    }
}

该应用程序崩溃并出现异常断点,将我指向带有if语句的行。关于这里可能有什么问题的任何想法?我只是想检查它VC是否存在以及是否存在 - 执行代码。

4

6 回答 6

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

for (int i = 0 ; i <viewControlles.count; i++){ 
  if ([YourVC isKindOfClass:[viewControlles objectAtIndex:i]]) {
          //Execute your code
      }
}
于 2013-01-17T12:32:22.723 回答
5
 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
          }
    }

只是为了一个想法类的containsObject:方法NSArray也可能有效。

于 2013-01-18T09:31:02.390 回答
3
-(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 
}
于 2014-12-23T17:19:23.380 回答
2
- (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];

     }
 }
于 2013-01-17T12:24:07.663 回答
1

您可以使用以下代码执行此操作(检查导航堆栈上的 ViewController 是否存在):

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)
于 2016-07-14T06:22:46.547 回答
0
NSArray *viewControlles = self.navigationController.viewControllers;

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

        //Enter your code

    }
}
于 2016-12-22T19:47:34.890 回答