Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertController将不显示-在Swift中_Ios_Swift_Uialertview_Mfmailcomposeviewcontroller_Uialertcontroller - Fatal编程技术网

Ios UIAlertController将不显示-在Swift中

Ios UIAlertController将不显示-在Swift中,ios,swift,uialertview,mfmailcomposeviewcontroller,uialertcontroller,Ios,Swift,Uialertview,Mfmailcomposeviewcontroller,Uialertcontroller,我已经创建了一个ContactUsViewController let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) alertController.addAction(okButton) controller.

我已经创建了一个ContactUsViewController

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)   
}
在该控制器中,用户将从pickerView中选择一个选项,然后在文本视图中键入一条消息,然后按下发送电子邮件按钮

当他们按下按钮时,会创建一个MFMailComposeViewController,以便他们可以发送电子邮件。现在,当电子邮件被发送、保存、取消或失败时,MFMailComposeViewController将关闭,因为它们将返回到我的应用程序中。我想要一个警报,然后出现给他们一个最新的消息,关于刚刚发生的事情。我最初使用UIAlertView进行设置,并将此代码放在fun MailComposecController函数中,请参见以下内容:

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

    }
如您所见,我已使用UIAlertView保存电子邮件,但电子邮件失败。这工作绝对正常,并显示出我的警惕性

我最近读到UIAlertView从iOS 8开始贬值,我们现在应该使用UIAlertController。因此,我尝试使用UIAlertController创建相同的东西,您可以在电子邮件发送部分看到。然而,这似乎不起作用,它只是没有显示任何警报。它会将此错误打印到日志中:

Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy! 
但我真的不确定这意味着什么,或者更重要的是,如何修复它

我的问题是:

我说我不能使用UIAlertView对吗? 从邮件应用返回后,如何修复此问题并使UIAlerController出现?
提前谢谢

问题是在调用以下函数时:

present(alertController, animated: true, completion: nil)
您的邮件视图控制器仍然可见。您必须确保alertController显示在窗口层次结构的顶部。在您的示例中,您具有以下窗口层次结构:

+---------------------------+
| MailComposeViewController |
+---------------------------+
            ||
+---------------------------+
|  ContactUsViewController  |
+---------------------------+
您应该做的是首先关闭电子邮件视图控制器。完成后,向您显示警报视图控制器

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)   
}
或者,您也可以在MailComposeViewController上显示alertController,如下所示:

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)
斯威夫特4

不再显示警报消息

毫无疑问

let AlertOnce = UserDefaults.standard
    if(!AlertOnce.bool(forKey: "oneTimeAlert")){


        let messageTitle = "Your selected branch is \(storeIDName)"
        let alertView = UIAlertController(title: messageTitle, message: "", preferredStyle: .actionSheet)
        let DoNotShowAgainAction = UIAlertAction(title: "Do Not Show Again", style: UIAlertActionStyle.default) { (action:UIAlertAction) in

            AlertOnce.set(true , forKey: "oneTimeAlert")
            AlertOnce.synchronize()

        }
        let change = UIAlertAction(title: "Change Branch", style: .default){(action) in
            let myVC = self.storyboard?.instantiateViewController(withIdentifier: "FindRestaurentViewControllerID") as! FindRestaurentViewController
            self.navigationController?.pushViewController(myVC, animated: true)
        }
        alertView.addAction(DoNotShowAgainAction)
        alertView.addAction(change)
        present(alertView, animated: true,completion: nil)
        alertView.setValue(NSAttributedString(string: messageTitle, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20),NSAttributedStringKey.foregroundColor : UIColor(red: 234/256, green: 104/256, blue: 26/256, alpha: 1)]), forKey: "attributedTitle")
        alertView.view.tintColor = UIColor.black

    }

我需要的是行动表而不是警报,由于我的具体情况,这里的解决方案都不起作用。如果您的UIAlertController未显示,另一种方法是始终将其作为Swift 4中最顶部的窗口:

func topmostController() -> UIViewController? {
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return nil
}

// Now put up the menu
let menu=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
menu.addAction(UIAlertAction(title: "Test", style: .default, handler: {_ in self.doTest()} ))
menu.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in self.doCancel()} ))
topmostController()?.present(menu, animated: true, completion: nil)

无论视图控制器堆栈如何,这都会显示窗口。

在DispachQueue.main.async{}中显示警报后,我现在面临同样的问题,代码运行正常