17

Sử dụng iOS Tôi có 15 ViewControllers bây giờ tôi muốn bật từ một ViewController sang một View Controller khác.Cách bật từ một bộ điều khiển chế độ xem sang một bộ điều khiển chế độ xem khác

Tôi đang sử dụng mã này:

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

Điều này cho thấy lỗi this ViewController not exist và sau đó tôi đang sử dụng mã này:

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

Mã này là đúng để bật từ thirdViewController để secondViewController. Nhưng chuyện gì xảy ra khi chúng ta bật từ IX (9) ViewController để Fifth (thứ 5) ViewController sau đó tôi đang sử dụng mã này trong IX (9) ViewController:

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

Nó không bật từ IX (9) ViewController để Fifth (5) ViewController ngoài rằng nó xuất hiện thứ chín (9) ViewController đến Eight (8) ViewController. Tôi không biết điều gì đã xảy ra khi chúng tôi sử dụng dòng này:

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

Khi chúng tôi sử dụng số này trong Ninth(9th)ViewController. NsLog hiển thị:

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

Tôi không biết tại sao chỉ có 4 Chế độ xem bộ điều khiển hiển thị. Bất cứ khi nào tôi đang sử dụng 15 Xem bộ điều khiển. Vấn đề này xảy ra trong mỗi bộ điều khiển xem. Ví dụ nếu tôi đang sử dụng hình thức pop mười lăm (15) ViewController để Fifth (5) ViewController sau đó cùng một vấn đề biểu hiện.

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

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

Tôi muốn đếm Số lượng ViewControllers và sau đó bật đến ViewController cụ thể.

+2

Bạn có chắc chắn rằng tất cả các ViewControllers được đẩy đến NavigationController không? – talnicolas

Trả lời

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

                break; 
            } 
        } 
1

Hãy thử như thế này

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

Đầu tiên:

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

Bạn không thể làm điều này vì bạn phân bổ một bộ điều khiển Sec nhìn mới đó không phải là trong một bộ điều khiển chuyển hướng.

Xem xét sử dụng này:

Bạn đang ở 9 view controller

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

Bạn không thể bật lên bộ điều khiển xem mới (giống như bạn làm với ví dụ secondViewController của bạn).

Khi sử dụng một UINavigationController bạn

Thêm điều khiển vào stack với:

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

Pop đến trước một với:

[self.navigationController popViewControllerAnimated:YES]; 

Pop đến một trước bộ điều khiển trong ngăn xếp (Phải đã bị đẩy trước đó):

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

Quay trở lại vào thư mục gốc Controller với

[self.navigationController popToRootViewControllerAnimated:YES]; 
6

Hãy thử điều này

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES]; 
1
BOOL check = FALSE; 
NSArray *viewControllers = [[self navigationController] viewControllers]; 
id obj; 
for(int i=0;i<[viewControllers count];i++) 
{ 
    obj=[viewControllers objectAtIndex:i]; 
    if([obj isKindOfClass:[yourclassname class]]) 
    { 
     check = TRUE; 
     break; 
    } 
} 

if (check) 
{ 

    [[self navigationController] popToViewController:obj animated:YES]; 
} 
else 
{ 
    yourclassname *yourclassnameObj=[self.storyboard instantiateViewControllerWithIdentifier:@"yourclassStoryBoardID"]; 
    [self.navigationController pushViewController:yourclassnameObj animated:true]; 

} 
1

Đối Swift 3.0 , sử dụng bộ lọc:

let desiredViewController = self.navigationController!.viewControllers.filter { $0 is YourViewController }.first! 
self.navigationController!.popToViewController(desiredViewController, animated: true) 
Các vấn đề liên quan