【问题标题】:Make two different buttons create two different email subjects制作两个不同的按钮创建两个不同的电子邮件主题
【发布时间】:2018-02-05 03:07:48
【问题描述】:

我试图让我用if indexPath.section == 1 && indexPath.row == 0if indexPath.section == 1 && indexPath.row == 1 显示的两个不同按钮在我提取电子邮件时具有不同的主题。两个按钮拉出同一个主题(建议:)。我将如何让程序区分它们?

import UIKit
import Foundation
import MessageUI

class AccountViewController: UITableViewController, MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.view.backgroundColor = .clear

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 && indexPath.row == 1{
            print("pressed")
            let mailComposeViewController2 = configureMailController()
            if MFMailComposeViewController.canSendMail() {
                self.present(mailComposeViewController2, animated: true, completion: nil)
            } else {
                showMailError()
            }
        }
        if indexPath.section == 1 && indexPath.row == 0 {
            print("pressed")
            let mailComposeViewController = configureMailController()
            if MFMailComposeViewController.canSendMail() {
                self.present(mailComposeViewController, animated: true, completion: nil)
            } else {
                showMailError()
            }
        }
    }
    //if indexPath.section == 1 && indexPath.row == 1 {

    //}
        func configureMailController() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self
            mailComposerVC.setToRecipients(["SolsticeOfficialLLC@gmail.com"])
            mailComposerVC.setSubject("Suggestion: ")

            return mailComposerVC
        }
        func configureMailController2() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self
            mailComposerVC.setToRecipients(["SolsticeOfficialLLC@gmail.com"])
            mailComposerVC.setSubject("Report: ")

            return mailComposerVC
        }
        func showMailError() {
            let sendMailErrorAlert = UIAlertController(title: "Could not send email", message: "Your device could not send email", preferredStyle: .alert)
            let dismiss = UIAlertAction(title: "OK", style: .default, handler: nil)
            sendMailErrorAlert.addAction(dismiss)
            self.present(sendMailErrorAlert, animated: true, completion: nil)
        }
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith: MFMailComposeResult, error: Error?){
            controller.dismiss(animated: true, completion: nil)
        }

}

【问题讨论】:

    标签: ios swift uitableview mfmailcomposeviewcontroller


    【解决方案1】:

    你需要添加一个参数来帮助你:

    func configureMailController(subject = "Unspecified Subject") -> MFMailComposeViewController {
                let mailComposerVC = MFMailComposeViewController()
                mailComposerVC.mailComposeDelegate = self
                mailComposerVC.setToRecipients(["SolsticeOfficialLLC@gmail.com"])
                mailComposerVC.setSubject(subject)
    
                return mailComposerVC
            }
    

    那么,在配置的时候随心所欲:

    let mailComposeViewController1 = configureMailController("Specific Subject 1")
    

    ....

    let mailComposeViewController2 = configureMailController("Specific Subject 2")
    

    【讨论】:

    • 你能举个例子说明在哪里添加这个参数吗?
    • 在代码中,我提供了第一个sn-p作为修改后的方法,带有一个名为subject的参数,在两个小sn-ps中如何通过传递调用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2014-12-30
    • 2021-08-12
    相关资源
    最近更新 更多