5

Tôi đã tạo một ContactUsViewController.UIAlertController sẽ không hiển thị - Trong Swift

Trong bộ điều khiển này, người dùng sẽ chọn một tùy chọn từ pickerView và sau đó nhập tin nhắn vào textView và nhấn nút Gửi email.

Khi họ nhấn nút, nó tạo ra một MFMailComposeViewController để họ có thể gửi email. Bây giờ, khi email được gửi, Đã lưu, Đã hủy hoặc Không thành công, các số MFMailComposeViewController sẽ đóng khi chúng trở lại trong ứng dụng của tôi. Tôi muốn một cảnh báo sau đó xuất hiện để cung cấp cho họ một bản cập nhật, về những gì từng xảy ra. Tôi đã cài đặt mặc định này bằng cách sử dụng một UIAlertView, và đã đặt mã này trong fun mailComposeController chức năng, xem dưới đây:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 

    switch result.rawValue { 

    case MFMailComposeResultCancelled.rawValue: 
     NSLog("Email cancelled") 
    case MFMailComposeResultSaved.rawValue: 
     NSLog("Email saved") 
     let sendMailErrorAlert = UIAlertView(title: "Email saved", message: "Your email has been saved in Mail.", delegate: self, cancelButtonTitle: "OK") 
     sendMailErrorAlert.show() 
    case MFMailComposeResultSent.rawValue: 
     NSLog("Email sent") 

     let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
     let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
     alertController.addAction(okButton) 
     presentViewController(alertController, animated: true, completion: nil) 

    case MFMailComposeResultFailed.rawValue: 
     NSLog("Email failed: %@", [error!.localizedDescription]) 
     let sendMailErrorAlert = UIAlertView(title: "Oops!", message: "Looks like something went wrong, and the email couldn't send. Please try again later.", delegate: self, cancelButtonTitle: "OK") 
     sendMailErrorAlert.show() 
    default: 
     break 

    } 

Như bạn thấy đấy, tôi đã sử dụng UIAlertView cho Email Saved và Email thất bại. Điều này hoạt động hoàn toàn tốt và hiển thị cảnh báo của tôi như mong đợi.

Gần đây tôi đã đọc rằng UIAlertView bị khấu hao từ iOS 8 và giờ chúng tôi nên sử dụng UIAlertController. Vì vậy, tôi đã thử tạo một điều tương tự bằng cách sử dụng UIAlertController, mà bạn có thể thấy cho phần Gửi email. Tuy nhiên, điều này dường như không hoạt động, nó chỉ không hiển thị bất kỳ cảnh báo nào. Nó in lỗi này vào nhật ký:

Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy! 

Nhưng tôi không thực sự chắc chắn điều này đang nói, hoặc quan trọng hơn là cách khắc phục.

Câu hỏi của tôi là:

  1. Am Tôi đúng khi nói tôi không thể sử dụng UIAlertView?
  2. Làm cách nào để khắc phục sự cố này và làm cho UIAlerController xuất hiện sau khi tôi đã trở về từ ứng dụng Thư?

Xin cảm ơn trước.

Trả lời

5

Vấn đề là do thời gian bạn gọi hàm sau này:

present(alertController, animated: true, completion: nil) 

điều khiển xem email của bạn vẫn còn nhìn thấy được. Bạn phải đảm bảo rằng alertController được trình bày ở trên cùng của hệ thống phân cấp cửa sổ. Trong ví dụ của bạn, bạn có hệ thống phân cấp cửa sổ sau:

+---------------------------+ 
| MailComposeViewController | 
+---------------------------+ 
      || 
+---------------------------+ 
| ContactUsViewController | 
+---------------------------+ 

Điều bạn nên làm là loại bỏ bộ điều khiển chế độ xem email trước. Khi điều đó được thực hiện, hãy cho bạn thấy bộ điều khiển xem cảnh báo.

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
alertController.addAction(okButton) 
controller.dismissViewControllerAnimated(true){() -> Void in 
    self.present(alertController, animated: true, completion: nil) 
} 

Ngoài ra, bạn cũng có thể trình bày alertController của bạn trên đầu trang của MailComposeViewController, như vậy:

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
alertController.addAction(okButton) 
controller.present(alertController, animated: true, completion: nil) 
+1

này làm việc tuyệt vời! cảm ơn rất nhiều! – Nick89

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