【发布时间】:2017-11-19 15:52:52
【问题描述】:
当我关闭从导航堆栈中的第三个 viewController 模态呈现的 MFMailComposeViewController 或 MFMessageComposeViewController 实例时,导航堆栈被重置,并且根 VC 被重新加载。如何防止这种行为并保留在原始呈现视图控制器(堆栈中的第三个 VC)上?无论我从呈现的 VC、呈现的 VC 还是 navigationController 调用dismiss,我都会得到相同的行为。
以前有人问过这个问题,但我没有看到解决方案。
应用结构如下所示:
TabBarController
Tab 1 - TripsNavController
-> Trips IntroductionVC (root VC) segue to:
-> TripsTableViewController segue to:
-> TripEditorContainerVC
- TripEditorVC (child of ContainerVC)
- HelpVC (child of ContainerVC)
Tab 2...
Tab 3...
Tab 4...
在 TripEditorVC 中,我展示了 MFMailComposeViewController。下面的函数是在 UIViewController 的扩展中声明的,它采用了 MFMailComposeViewControllerDelegate 协议
func shareWithEmail(message: NSAttributedString) {
guard MFMailComposeViewController.canSendMail() else {
showServiceError(message: "Email Services are not available")
return
}
let composeVC = MFMailComposeViewController()
composeVC.setSubject("My Trip Plan")
composeVC.setMessageBody(getHTMLforAttributedString(attrStr: message), isHTML: true)
composeVC.mailComposeDelegate = self
present(composeVC, animated: true, completion: nil)
}
然后在委托方法中我关闭 MFMailComposeVC:
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .sent:
print("Mail sent")
case .saved:
print("Mail saved")
case .cancelled:
print("Mail cancelled")
case .failed:
print("Send mail failed")
}
if error != nil {
showServiceError(message: "Error: \(error!.localizedDescription)")
}
dismiss(animated: true, completion: nil)
}
我尝试了以下方法来呈现和关闭并获得相同的行为,即:TripsNavController 清除导航堆栈并将 TripsIntroductionVC 作为其根 VC 重新加载:
self.present(composeVC, animated: true, completion: nil)
self.parent?.present(composeVC, animated: true, completion: nil)
self.parent?.navigationController?.present(composeVC, animated: true, completion: nil)
self.navigationController?.present(composeVC, animated: true, completion: nil)
【问题讨论】:
-
您需要提供一些实际代码,因为我们无法知道您做错了什么。 Dismiss 逻辑的工作原理取决于调用者是谁以及调用者是否提供了某些内容。
-
Dima - 用代码查看已编辑的问题。
-
你试过
presentingViewController?.dismiss(self, animated: true)吗? -
对于呈现的 MFMailComposeViewController,presentingViewController 的计算结果为 nil。对于来自同一父级的其他呈现的 viewController,presentingViewController 的计算结果是顶部的 TabBarController。当我关闭 MFMailComposeViewController 时,整个堆栈被重置为 root。当任何其他呈现的 viewController 被解除时,堆栈不会重置。