2013-03-08 23 views
30

Trong một trong các ứng dụng của tôi, tôi đang gọi chế độ xemController từ phương thức application didReceiveLocalNotification. Trang tải thành công, nhưng nó cho thấy một cảnh báo như:Cảnh báo: cố gắng trình bày ViewController mà chế độ xem không nằm trong hệ thống phân cấp cửa sổ

Warning: Attempt to present <blankPageViewController: 0x1fda5190> on 
<ViewController: 0x1fd85330> whose view is not in the window hierarchy! 

Mã của tôi là như sau:

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

    blankPageViewController *myView = [[blankPageViewController alloc] 
       initWithNibName:@"blankPageViewController" bundle: nil]; 
    myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.viewController presentViewController:myView animated:NO completion:nil]; 
} 
+0

Khi bạn đang gọi didReceiveLocalNotification? –

+0

@Bhargavi: nó là một deligate được gọi khi nhận được thông báo địa phương. –

Trả lời

21

Cuối cùng tôi đã giải quyết vấn đề đó bằng mã sau.

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     self.blankviewController = [[blankPageViewController alloc] initWithNibName:@"blankPageViewController" bundle:nil]; 
     self.window.rootViewController = self.blankviewController; 
     [self.window makeKeyAndVisible]; 
} 
0

Nếu loại ứng dụng id UINavigationController của bạn thì bạn có thể sử dụng.

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

blankPageViewController *myView = [[blankPageViewController alloc] 
            initWithNibName:@"blankPageViewController" bundle: nil]; 
myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self.navigationController presentViewController:myView animated:NO completion:nil]; } 

Hy vọng điều này sẽ giúp bạn. Tất cả tốt nhất !!!

2

nó có thể là do quan điểm của viewController hiện không tải trong hệ thống cấp bậc cửa sổ khi VC được trình bày ...

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController; 
    rootViewController = [[navigationController viewControllers] objectAtIndex:0]; 
    blankPageViewController *myView = [[blankPageViewController alloc] initWithNibName:@"blankPageViewController" bundle: nil]; 
    [rootViewController presentModalViewController:myView animated:YES]; 
} 
+0

Cảm ơn lời khuyên. Tôi đã thử điều này. Nó làm việc tốt lần đầu tiên, nếu thông báo xuất hiện một lần nữa, nó được hiển thị cùng một cảnh báo ... :( –

+0

sửa ans nên làm việc ... – BhushanVU

12

Theo giả thiết của tôi, tôi cảm thấy như bạn đang cố gắng để trình bày myView từ self.viewController trước self.viewController được đính kèm hoặc được đặt trong cấu trúc phân cấp cửa sổ. Vì vậy, hãy đảm bảo hiển thị myView sau self.viewController xuất hiện/được đính kèm vào cửa sổ.

Why can't a modal view controller present another in viewDidLoad?

+1

Yep ... di chuyển vào viewDidAppear hoàn toàn giải quyết nó cho tôi! –

2

Tôi đã có một vấn đề tương tự, tôi đã gọi presentViewController từ bên trong AppDelegate.m tôi khi ứng dụng vào foreground:

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    NSDate * lastActive = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastActive"]; 

    double interval = [[NSDate date] timeIntervalSinceDate:lastActive]; 

    NSLog(@"Time Interval: %f", interval); 

    if (interval > 900) { // interval is in seconds, so 900 seconds = 15 mins 
     Login *pres = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"login"]; 

     UINavigationController *navi = ((UINavigationController*)self.window.rootViewController); 
     if (navi.presentedViewController) { 
      [navi dismissViewControllerAnimated:YES completion:^{ 
       [navi presentViewController:pres animated:NO completion:nil]; 
      }]; 
     } else { 
      [navi presentViewController:pres animated:NO completion:nil]; 
     } 
    } 
} 

này làm việc không có vấn đề vì nó gạt bỏ xem bộ điều khiển đầu tiên (bất kể khung nhìn nào đã được đẩy lên nó) nếu nó có mặt trước khi trình bày bộ điều khiển xem Đăng nhập. Nếu không thì tôi chỉ có thể trình bày bộ điều khiển xem Đăng nhập ngay lập tức.

15

Đặt

[self.viewController presentViewController:myView animated:NO completion:nil]; 

vào một chức năng ví dụ

- (void)yourNewFunction 
{ 
    [self.viewController presentViewController:myView animated:NO completion:nil]; 
} 

và sau đó gọi nó như thế này:

[self performSelector:@selector(yourNewFunction) withObject:nil afterDelay:0.0]; 

Vấn đề đã được mô tả here và tại sao performSelector:withObject:afterDelay sửa chữa vấn đề này? Vì bộ chọn sẽ không được gọi cho đến lần chạy tiếp theo của vòng lặp chạy. Vì vậy, mọi thứ có thời gian để giải quyết xuống và bạn sẽ chỉ bỏ qua một vòng lặp chạy.

1

Cạnh Trường hợp:

Một số người có thể tìm thấy điều này thông qua Google và nó là giá trị chỉ ra rằng đôi khi lỗi này được nâng lên nếu bạn đang tải một cái nhìn lên như bộ điều khiển xem gốc mà không được dán nhãn là quan điểm ban đầu trong Storyboard (& một chế độ xem khác) và sau đó bạn tải một chế độ xem khác lên trên cùng. Ví dụ: trong Chế độ xem đăng nhập được tải theo chương trình từ trong phương thức applicationdidfinish... của Đại biểu ứng dụng.Đơn giản chỉ cần thay đổi chế độ xem ban đầu (kéo mũi tên trong Bảng phân cảnh) đến một mũi tên phù hợp cho cấu trúc phân cấp của bạn. Bí truyền, mơ hồ, nhưng đáng chú ý.

0

Đối Swift 2.0 tôi đã sử dụng ghi đè cho viewDidAppear với:

let vc = self.storyboard .instantiateViewControllerWithIdentifier ("Second") là?! SecondViewController

self.presentViewController(vc, animated: true, completion: nil) 

Trong đó "Thứ hai" là ID bảng phân cảnh của SecondViewController trong trình kiểm tra danh tính.

0
I have used Navigation Controller and on which i have used a 
ModalViewController to present a Popup(Present over current context). 
From this Popup i am opening a ViewController Modally which 
caused the Warning. So to resolve i did below change: 

[self.presentedViewController performSegueWithIdentifier:@"PresentScreenSegueId" sender:sender]; 
Các vấn đề liên quan