由于层次结构的原因,无法显示由 MFMailComposeViewController 创建的页面 - Swift 4



我尝试添加一个函数,即用户触摸表中的一行后会弹出一个邮件页面。也就是说,这意味着用户可以在点击该行时激活"功能"(此处该功能的名称为"orderOfSendAnEmailToReportTheProblem"(。我的所有代码都如下所示。(这种代码已经被Stackoverflow上的几个天才提出了......

import Foundation
import UIKit
import MessageUI
class ReportProblem : UIViewController, MFMailComposeViewControllerDelegate {
func orderOfSendAnEmailToReportTheProblem() {
let mailComposeViewController = configureMailController()
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: true, completion: nil)
} else {
showMailError()
}
}
//Activate the series of the commands of sending the email.

func configureMailController() -> MFMailComposeViewController {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients(["my email"])
mailComposeVC.setSubject("Yo")
return mailComposeVC
}
//Set the recipient and the title of this email automatically.
func showMailError() {
let sendMailErrorAlert = UIAlertController(title: "Could not sned the email.", message: "Oops, something was wrong, please check your internet connection once again.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
sendMailErrorAlert.addAction(dismiss)
self.present(sendMailErrorAlert, animated: true, completion: nil) //If you conform the protocol of NSObject instead of UIViewController, you could not finish this line successfully.
}
//Set a alert window so that it would remind the user when the device could not send the email successfully.
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
//Set this final step so that the device would go to the previous window when you finish sending the email.
}

但是,出现了一个问题。当我在我的真实设备上测试它时,在我点击该特定行后,没有任何反应,没有弹出任何新页面......Xcode 仅显示"警告:尝试显示其视图不在窗口层次结构中!我已经尝试了几种方法,例如"view.bringSubview(toFront: mailComposeVC("或在我的代码末尾添加下面显示的代码,但没有任何效果。

func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}

我注意到其他人在想要创建警报窗口时也会遇到类似的问题,其解决方案是创建一个独立的 UIWindow,但我想使用 mailComposeController 来呈现电子邮件页面。其他一些人也面临着一些关于MFMailComposeViewController的问题,但他们的问题与层次结构无关。我是斯威夫特的新手,我被这个问题困扰了一整天......我使用 swift 4 开发我的应用程序,有人知道如何在这里解决这个问题吗?...

所以现在我正在编写另一种呈现方式,我将其用于通用视图。 在另一个类中有一些代码来表示视图,以便您可以使用这两种方法在整个应用中重用它们。

func slideInFromRight(parentView:UIView,childView:UIView) {
childView.transform = CGAffineTransform(translationX: parentView.frame.maxX, y: 0)
parentView.addSubview(childView)
UIView.animate(withDuration: 0.25, animations: {
childView.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
func slideOutToRight(view:UIView) {
UIView.animate(withDuration: 0.25, animations: {
view.transform = CGAffineTransform(translationX: view.frame.maxX, y: 0)
},completion:{(completed:Bool) in
view.removeFromSuperview()
})
}

现在使用这些方法来呈现和删除自定义视图控制器,如下所示

let window = UIApplication.shared.keyWindow
let vc = YourViewController().instantiate()
self.addChildViewController(vc)
let view = vc.view
view.frame = CGRect(x: 0, y: 20, width: window!.frame.width, height: window!.frame.height-20)
//Here Animation is my custom presenter class and shared is it's shared instance.
Animation.shared.slideInFromRight(parentView: window!, childView: view)
//Or you can use current View controller's view 
Animation.shared.slideInFromRight(parentView: self.view!, childView: view)

天才维韦克·辛格,你的方式看起来不错,但有点乏味。而且,它在我的项目中仍然不起作用...(似乎您使用了一些关于UIView的代码,例如parentViewchildViewview。但是,我使用了MFMailComposeViewController似乎与原始视图略有不同......我不确定这个理论是否正确...

但是,我已经找到了解决方案。我认为问题是用户单击另一个tableViewController中的行(这里是SettingTVController(,它将激活"另一个"viewController中的函数"orderOfSendAnEmailToReportTheProblem( )"(这里是ReportProblem(。因为有两个不同的视图控制器,所以发生了某种冲突。

因此,我将我在上述问题中发布的整个代码移动到我原来的 tableViewController,这样用户在激活该功能时就不会进入另一个 viewController,并且不再有层次结构问题。

import UIKit
import StoreKit
import MessageUI
class SettingTVController: UITableViewController, MFMailComposeViewControllerDelegate {
var settingTitleConnection = showData()
override func viewDidLoad() {
//skip
}
override func didReceiveMemoryWarning() {
//skip
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//skip
}
override func numberOfSections(in tableView: UITableView) -> Int {
//skip
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//skip
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.indexPathForSelectedRow?.row == 2 && tableView.indexPathForSelectedRow?.section == 1 {
orderOfSendAnEmailToReportTheProblem()
} else {
//skip
}
tableView.deselectRow(at: indexPath, animated: true)
}
//-----<The codes below is used to construct the function of reporting problem with email>-----
func orderOfSendAnEmailToReportTheProblem() {
let mailComposeViewController = configureMailController()
self.present(mailComposeViewController, animated: true, completion: nil)
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: false, completion: nil)
} else {
showMailError()
}
}
//Activate the series of the commands of sending the email.
func configureMailController() -> MFMailComposeViewController {
let mailComposeVC = MFMailComposeViewController()
mailComposeVC.mailComposeDelegate = self
mailComposeVC.setToRecipients(["datototest@icloud.com"])
mailComposeVC.setSubject("Reporting of Problems of Rolling")
return mailComposeVC
}
//Set the recipient and the title of this email automatically.
func showMailError() {
let sendMailErrorAlert = UIAlertController(title: "Could not send the email.", message: "Oops, something was wrong, please check your internet connection once again.", preferredStyle: .alert)
let dismiss = UIAlertAction(title: "Ok", style: .default, handler: nil)
sendMailErrorAlert.addAction(dismiss)
self.present(sendMailErrorAlert, animated: true, completion: nil) //If you conform the protocol of NSObject instead of UIViewController, you could not finish this line successfully.
}
//Set a alert window so that it would remind the user when the device could not send the email successfully.
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
//UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
}
//Set this final step so that the device would go to the previous window when you finish sending the email.
//-----<The codes above is used to construct the function of reporting problem with email>-----
}

我在上面发布了我的代码,以便有一天可以帮助其他面临类似问题的人。再次感谢您的帮助!!

我不知道为什么您面临视图层次结构问题。但是我能够在 swift 4 中实现邮件共享选项。我遵循了完全相同的步骤。

  1. 检查是否可以发送邮件:

    MFMailComposeViewController.canSendMail((

  2. 配置邮件正文:

    private func configureMailController() -> MFMailComposeViewController {
    let mailComposeViewController = MFMailComposeViewController()
    mailComposeViewController.mailComposeDelegate = self
    mailComposeViewController.setMessageBody("MESSAGE BODY", isHTML: true)
    return mailComposeViewController
    

    }

  3. 出示邮件 VC:

    present(mailComposeViewController, animated: true)

  4. 确认可选协议并显式关闭视图:

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
    

    }

最新更新