代码之家  ›  专栏  ›  技术社区  ›  Alex Smith

移动到下一个视图控制器后如何显示警报?

  •  0
  • Alex Smith  · 技术社区  · 10 月前

    我正在使用此代码进行呼叫 MFMailComposeViewController 另一类:

    SomeViewController:

    class SomeController: UIViewController {
        func contactWithDeveloper() {
            let detailController = Mail()
            detailController.messageTitle = "1"
            detailController.messageAddress = "1"
            detailController.messageText = "1"
            detailController.errorTitle = "1"
            detailController.errorMessage = "1"
            detailController.modalPresentationStyle = .overFullScreen
            self.present(detailController, animated: false, completion: nil)
        }
    }
    

    MailViewController:

    class Mail: UIViewController, MFMailComposeViewControllerDelegate {
        
        var messageTitle = ""
        var messageAddress = ""
        var messageText = ""
        var errorTitle = ""
        var errorMessage = ""
        
        override func viewDidLoad() {
            super.viewDidLoad()
            send(title: messageTitle, address: [messageAddress], message: messageText, errorTitle: errorTitle, errorMessage: errorMessage)
        }
        
        func send(title: String, address: [String], message: String, errorTitle: String, errorMessage: String) {
            if MFMailComposeViewController.canSendMail() {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                mail.setSubject(title)
                mail.setToRecipients(address)
                mail.setMessageBody(message, isHTML: false)
                present(mail, animated: true, completion: nil)
            } else {
                let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: dismissHandler))
                self.present(alert, animated: true, completion: nil)
            }
        }
        
        func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            controller.dismiss(animated: true, completion: nil)
            dismiss(animated: false, completion: nil)
        }
        
        func dismissHandler(aler: UIAlertAction) {
            dismiss(animated: false, completion: nil)
        }
    }
    

    此代码片段 if MFMailComposeViewController.canSendMail() 工作得很好,我能看到 MFMailComposeViewController 。但如果我不能发信息 UIAlertController 未被呼叫,我没有看到警报。如何解决这个问题?

    1 回复  |  直到 10 月前
        1
  •  1
  •   Sweeper    10 月前

    现在发布警报还为时过早 viewDidLoad .你可以用 viewWillAppear viewDidAppear .

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if MFMailComposeViewController.canSendMail() {
            // ...
        } else {
            let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: dismissHandler))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    推荐文章