当前位置:  开发笔记 > IOS > 正文

Swift 2:电子邮件取消按钮在第二次尝试表视图后停止工作

如何解决《Swift2:电子邮件取消按钮在第二次尝试表视图后停止工作》经验,为你挑选了1个好方法。

我正在使用一个表视图,其中有一个单元格,上面写着"向我们发送反馈".它打开了电子邮件应用程序,其中包含要发送的预设信息.我可以发送电子邮件和/或取消它,但是当我第一次取消它时,留在桌面视图,再次点击单元格打开电子邮件,我无法再取消它.它只停留在电子邮件视图中.

建议?

我的代码如下:

import UIKit
import MessageUI

class FeedbackViewController: UITableViewController, MFMailComposeViewControllerDelegate {

    let mailComposerVC = MFMailComposeViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.section == 0 && indexPath.row == 0
        {
            let alertController = UIAlertController(title: "Rate Us", message: "\nAre you enjoying our app? Please rate us in the app store!\n\nElse if you know of ways we can make our app better, please send us feedback so we can improve the experience for you!\n\nThank you!\n\nTimmy Caish", preferredStyle: .Alert)

            alertController.addAction(UIAlertAction(title: "Rate on iTunes", style: .Default, handler: {
                (action: UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL((NSURL(string: "http://google.com")!))
                print("Rate us alert button worked.")
                print("Send to iTunes")
            }))

            alertController.addAction(UIAlertAction(title: "Send Us Feedback", style: .Default, handler: {
                (action: UIAlertAction!) in
                print("Rate Us feeback button worked.")
                let mailComposeViewController = self.configureMailComposeViewController()
                if MFMailComposeViewController.canSendMail()
                {
                    self.presentViewController(mailComposeViewController, animated: true, completion: nil)
                }
                else
                {
                    self.showSendMailErrorAlert()
                }
            }))

            alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction) in
                print("Rate us cancel button worked.")
            print("Rate us")
            }))

            presentViewController(alertController, animated: true, completion: nil)
        }


        if indexPath.section == 0 && indexPath.row == 1
        {
            let mailComposeViewController = configureMailComposeViewController()

            if MFMailComposeViewController.canSendMail()
            {
                self.presentViewController(mailComposeViewController, animated: true, completion: nil)
            }
            else
            {
                self.showSendMailErrorAlert()
            }

            print("Send us feedback")
        }

    }

    func configureMailComposeViewController() -> MFMailComposeViewController {

        mailComposerVC.mailComposeDelegate = self

        mailComposerVC.setToRecipients(["developer.timmy@gmail.com"])
        mailComposerVC.setSubject("Weather Simplicity Feedback")
        mailComposerVC.setMessageBody("Hello,\n\nI would like to share the following feedback...\n\n", isHTML: false)

        return mailComposerVC

    }

    func showSendMailErrorAlert() {

        let sendMailErrorAlert = UIAlertController(title: "Error", message: "Your device could not send the email. Check your email configurations and try again.", preferredStyle: UIAlertControllerStyle.Alert)
        let okay = UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil)
        sendMailErrorAlert.addAction(okay)
        self.presentViewController(sendMailErrorAlert, animated: true, completion: nil)
    }

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

        switch result {

        case MFMailComposeResultCancelled:
            print("Cancelled mail")
            break
        case MFMailComposeResultSent:
            print("Message sent")
            break
        default:
            break
        }

        self.dismissViewControllerAnimated(true, completion: nil)

    }

}

Dallas Johns.. 5

尝试仅在didSelectRow中需要时创建新的mailViewController,而不是创建视图控制器.这将确保每次都是干净的,除非需要,否则不会创建,并且可以解决第二次取消不起作用的问题.



1> Dallas Johns..:

尝试仅在didSelectRow中需要时创建新的mailViewController,而不是创建视图控制器.这将确保每次都是干净的,除非需要,否则不会创建,并且可以解决第二次取消不起作用的问题.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有