10

Tôi gặp phải lỗi này khi tôi gọi phương thức dismissView của mình. Đây là phương thức:'Đã cố gắng bật đến trình điều khiển chế độ xem không tồn tại.'

-(IBAction)dismissView 
{ 
    RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
    [self.navigationController popToViewController:rootController animated:YES]; 
} 

Điều đó sẽ hoạt động và tôi đã kiểm tra, rootController được khởi tạo và phân bổ. Bất kỳ ý tưởng?

Trả lời

20

Tôi có vấn đề này thời gian gần đây và giải quyết với một cái gì đó như thế này ...

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

cảm ơn, hoạt động trên trường hợp của tôi – tesmojones

+0

nhanh chóng 3 thực hiện 'hãy _ = .popToViewController self.navigationController ((self.navigationController .viewControllers [1]) như HomeViewController, hoạt hình:?! True)?' –

10

-popToViewController được sử dụng để bật bộ điều khiển chế độ xem TẮT ngăn xếp, xuống ngăn xếp đã tồn tại. UINavigationController của bạn có một chồng ViewControllers (được lưu trữ trong thuộc tính viewControllers), khi bạn popToViewController, bạn sẽ muốn chuyển một trong các phần tử trong mảng đó làm đối số đầu tiên.

gì bạn có thể muốn làm nhất trong trường hợp này là sử dụng -popViewControllerAnimated:, sẽ loại bỏ các đầu ViewController từ stack

4

Bạn đang phân bổ RootViewController ngay tại đó. Nó không tồn tại trong ngăn xếp của bộ điều khiển điều hướng, vì vậy cho dù bạn có bật bao xa, bạn sẽ không đạt được nó.

10

tôi giải quyết điều này bằng cách sử dụng pushViewController hơn popToViewController

4

Nếu bạn đang sử dụng Storyboads, sử dụng segue này:

#import "PopToControllerSegue.h" 

@implementation PopToControllerSegue 

- (void) perform 
{ 
    UIViewController *sourceViewController = (UIViewController *)self.sourceViewController; 
    UIViewController *destinationViewController = (UIViewController *)self.destinationViewController; 

    for (UIViewController* controller in sourceViewController.navigationController.viewControllers) { 
     if ([controller isKindOfClass:destinationViewController.class]) { 
      [sourceViewController.navigationController popToViewController:controller animated:YES]; 
      return; 
     } 
    } 

    NSLog(@"PopToControllerSegue has failed!"); 
} 

@end 
0

Khi sử dụng Push Segues, bạn có thể dễ dàng quay lại thư mục gốc bằng cách sử dụng phương pháp này:

[self.navigationController popToRootViewControllerAnimated:YES]; 

Khi sử dụng phương thức Segues (vì từ bỏ trong câu hỏi và như là một tài liệu tham khảo chung), bạn có thể dismiss bộ điều khiển xem sử dụng phương pháp này:

[self dismissViewControllerAnimated:YES completion:nil]; 
1

Các UINavigationController có một chồng ViewControllers được lưu trữ trong thuộc tính viewControllers (NSArray). Liệt kê theo yêu cầu ViewController và bật đến số ViewController.

Mã sau đây sẽ giải quyết được sự cố.

-(IBAction)dismissView 
{ 
    NSArray *array = self.navigationController.viewControllers; 
    for (id controller in array) { 
     if ([controller isKindOfClass:[RootViewController class]]) { 
      [self.navigationController popToViewController:controller animated:YES]; 

     } 
    } 
} 
Các vấn đề liên quan