2016-01-09 18 views
8

Tôi muốn hiển thị thông báo cảnh báo từ viewDidLoad() phương thức ViewController.m thay vì phương thức viewDidAppear().Hiển thị thông báo cảnh báo từ viewDidLoad

Đây là mã của tôi:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //A SIMPLE ALERT DIALOG 
    UIAlertController *alert = [UIAlertController 
           alertControllerWithTitle:@"My Title" 
           message:@"Enter User Credentials" 
           preferredStyle:UIAlertControllerStyleAlert]; 


    UIAlertAction *cancelAction = [UIAlertAction 
           actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction *action) 
           { 
            NSLog(@"Cancel action"); 
           }]; 

    UIAlertAction *okAction = [UIAlertAction 
          actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction *action) 
          { 
           NSLog(@"OK action"); 
          }]; 

    [alert addAction:cancelAction]; 
    [alert addAction:okAction]; 
    [self presentViewController:alert animated:YES completion:nil]; 
} 

và tôi nhận được lỗi này:

Warning: Attempt to present <UIAlertController: 0x7fbc58448960> on <ViewController: 0x7fbc585a09d0> whose view is not in the window hierarchy!

+1

Bản sao có thể có của [chế độ xem không có trong hệ thống phân cấp cửa sổ] (http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy) – Woodstock

+0

@Woodstock câu hỏi của tôi khác ... –

Trả lời

12

OK không phải là lỗi, vấn đề là trong viewDidLoad phân cấp khung nhìn chưa được thiết lập đầy đủ. Nếu bạn sử dụng viewDidAppear, thì cấu trúc phân cấp được thiết lập.

Nếu bạn thực sự muốn gọi cảnh báo này trong chế độ xemDidLoad, bạn có thể thực hiện việc này bằng cách gói cuộc gọi trình bày trong khối GCD này để trì hoãn, đợi vòng lặp tiếp theo, tuy nhiên tôi khuyên bạn không nên xấu xí).

dispatch_async(dispatch_get_main_queue(),^{ 
    [self presentViewController:alert animated:YES completion:nil]; 
}); 
+1

Cảm ơn câu trả lời của bạn .. Điều này hoạt động :) –

+1

niềm vui của tôi, chỉ là một lưu ý, mặc dù các công trình trên, tôi đoán Apple không có ý định được trình bày trong viewDidLoad, vì nó rất sớm trong vòng đời viewController. – Woodstock

+0

Ok .. Tôi giữ điểm này trong đầu mình –

0

Bạn cần phải nhúng một điều khiển chuyển hướng và trình bày bộ điều khiển

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //A SIMPLE ALERT DIALOG 


    UIAlertController *alert = [UIAlertController 
          alertControllerWithTitle:@"My Title" 
          message:@"Enter User Credentials" 
          preferredStyle:UIAlertControllerStyleAlert]; 


    UIAlertAction *cancelAction = [UIAlertAction 
          actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
          style:UIAlertActionStyleCancel 
          handler:^(UIAlertAction *action) 
          { 
           NSLog(@"Cancel action"); 
          }]; 

    UIAlertAction *okAction = [UIAlertAction 
         actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction *action) 
         { 
          NSLog(@"OK action"); 
         }]; 

    [alert addAction:cancelAction]; 
    [alert addAction:okAction]; 

    [self.navigationController presentViewController:alert animated:NO completion:nil]; 
    // [self presentViewController:cameraView animated:NO completion:nil]; //this will cause view is not in the window hierarchy error 

} 

HOẶC

[self.view addSubview:alert.view]; 
    [self addChildViewController:alert]; 
    [alert didMoveToParentViewController:self]; 
+0

tôi thử mã được đề xuất của bạn. nhưng ứng dụng bị lỗi khi tôi nhấp vào bất kỳ nút nào tức là OK, Huỷ .. và cũng có Hộp cảnh báo được hiển thị ở phía trên bên trái –

+0

@ShivaniArorra tôi đã cập nhật kiểm tra ans của mình mà bạn cần thực hiện '[self.navigationController presentViewController: alert animated : NO hoàn thành: nil]; ' –

8

để chuyển cuộc gọi này sang phương thức viewDidAppear:.

+0

bạn nói đúng, tôi nghĩ rằng điều này nên được gọi từ viewDidLoad mặc dù. – Woodstock

+0

nhưng tôi muốn gọi dạng viewDidLoad: phương thức –

-1

Swift 3 iOS 10, tôi đã sử dụng hàng đợi hoạt động để đặt khối cập nhật giao diện người dùng lên chuỗi chính.

import UIKit 

class ViewController2: UIViewController { 

var opQueue = OperationQueue() 

override func viewDidLoad() { 
    super.viewDidLoad() 
     let alert = UIAlertController(title: "MESSAGE", message: "HELLO WORLD!", preferredStyle: UIAlertControllerStyle.alert) 
     // add an action (button, we can add more than 1 buttons) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
     // show the alert 
     self.opQueue.addOperation { 
      // Put queue to the main thread which will update the UI 
      OperationQueue.main.addOperation({ 
       self.present(alert, animated: true, completion: nil) 
      }) 
     } 
    } 
} 

Tóm lại, chúng tôi đang sử dụng không đồng bộ. Điều này cho phép thông điệp cảnh báo được hiển thị như mong đợi (ngay cả khi chúng ta đang ở trong viewDidLoad()).

+0

Điều này thực sự là thêm một khối vào hàng đợi hoạt động cục bộ để thêm một khối khác vào hàng đợi hoạt động chính sau đó trình bày cảnh báo. Sử dụng hàng đợi công văn là điều bình thường hơn: 'DispatchQueue.main.async {...}'. –

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