如何从一个视图控制器弹出到另一个视图控制器

时间:2013-07-25 13:43:29

标签: iphone ios objective-c uinavigationcontroller uipopovercontroller

使用iOS我现在有15个ViewControllers我想从一个ViewController弹出到另一个View Controller。

我正在使用此代码:

SecondViewController *Sec=[SecondViewController alloc]init];
[self.navigationController popViewController:Sec animated:YES];

这显示错误this ViewController not exist,然后我使用此代码:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

此代码适用于从thirdViewController弹出到secondViewController。但是当我们从第九个(第9个)ViewController弹出到第五个(第五个)ViewController然后我在第九个(第9个)ViewController中使用这个代码时发生了什么:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:4] animated:YES];

它没有从第九(9)ViewController弹出到第五(第五)ViewController,它将第九(9)ViewController弹出到第八(第8)ViewController。我不知道当我们使用这一行时发生了什么:

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

当我们在Ninth(9th)ViewController中使用它时。 NsLog显示:

array=   First(1st)ViewController;  
         Second(2nd)ViewController;
         Eight(8th)ViewController;
         Ninth(9th)ViewController;

我不知道为什么只有四个视图控制器显示。每当我使用15个视图控制器时。每个视图控制器中都会出现此问题。例如,如果我使用pop形式第十五(15)ViewController到Fifth(5th)ViewController,那么相同的问题就会出现。

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

array=     First(1st)ViewController;  
           Second(2nd)ViewController;
           fourteenth(14th)ViewController;
           fifteenth(15th)ViewController;

我想计算ViewControllers的数量,然后弹出到特定的ViewController。

8 个答案:

答案 0 :(得分:36)

您无法弹出到新的视图控制器(就像使用secondViewController示例一样)。

使用UINavigationController时

将控制器添加到堆栈中:

[self.navigationController pushViewController:<yournewViewController> animated:YES];

使用以下内容弹出上一个

[self.navigationController popViewControllerAnimated:YES];

弹出到堆栈中的前一个控制器(必须先被推送):

[self.navigationController popToViewController:<ViewControllerToPopTo> animated:YES];

使用

返回根控制器
[self.navigationController popToRootViewControllerAnimated:YES];

答案 1 :(得分:31)

for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
            {
                [self.navigationController popToViewController:controller animated:YES];

                break;
            }
        }

答案 2 :(得分:7)

试试这个

 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

答案 3 :(得分:5)

首先:

 SecondViewController *Sec=[SecondViewController alloc]init];
 [self.navigationController popViewController:Sec animated:YES];

您无法执行此操作,因为您分配了一个不在导航控制器中的新Sec视图控制器。

考虑使用:

你在9视图控制器

for (int i= 0 ; i < [[self.navigationController viewControllers]count] ; i++) {
    if ( [[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[FifiViewControllerClassname class]]) {
        [self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
    }
}

答案 4 :(得分:2)

Swift 4.0

 for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: HomeViewController.self) {
                self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }

答案 5 :(得分:1)

试试这个

MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray    
arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc]; 

答案 6 :(得分:1)

Public Sub btnRun_Click(sender As System.Object, e As System.EventArgs) Handles btnRun.Click

    Dim xlApp As Excel.Application
    Dim xlWorkBook1 As Excel.Workbook ' Interactions
    Dim xlWorkBooks As Excel.Workbooks
    Dim MainSheet1 As Excel.Worksheet

    xlApp = New Excel.Application
    xlWorkBooks = xlApp.Workbooks
    xlWorkBook1 = xlWorkBooks.Open(File1_name)
    xlApp.Visible = False

    MainSheet1 = xlWorkBook1.Sheets(1)
    MainSheet1.Activate()

    Dim InteractionRows As Long = MainSheet1.UsedRange.Rows.Count ' Total number of rows in the Interaction worksheet
    Dim Duplicate_Found_Index As Long ' Stores all match index values for duplicate interactions
    Dim Duplicate_ID As Long ' Defining the first column to loop through and match duplicate interactions

    ' **** Duplicate Interaction ****
    ' Flag Creation
    For Duplicate_ID = 1 To (InteractionRows)
        If MainSheet1.Cells(Duplicate_ID, 1) <> "" Then 'checking if the cell is having any item, skipping if it is blank.
            Duplicate_Found_Index = xlApp.WorksheetFunction.Match(MainSheet1.Cells(Duplicate_ID, 1), MainSheet1.Range("A1:A" & InteractionRows), 0) 'getting match index number for the value of the cell.
            If Duplicate_ID <> Duplicate_Found_Index Then 'if the match index is not equals to current row number, then it is a duplicate value
                MainSheet1.Cells(Duplicate_ID, 34) = "1" ' Print the flag "1" in the 34th column if duplicate found
            Else
                MainSheet1.Cells(Duplicate_ID, 34) = "0" ' Print the flag "0" in the 34th column if no duplicate
            End If
        End If
    Next
End Sub

答案 7 :(得分:1)

对于Swift 3.0 ,请使用过滤器:

let desiredViewController = self.navigationController!.viewControllers.filter { $0 is YourViewController }.first!
self.navigationController!.popToViewController(desiredViewController, animated: true)