为不同的按钮创建一个单独的主题?
Creating a separate subject for a different button?
我试图让 if indexPath.section == 1 && indexPath.row == 0
和 if 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)
}
}
您在两个按钮上调用了相同的方法。像这样更改您的代码。
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 = configureMailController2() //Your error was in this line
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)
}
}
您正在为两行调用 configureMailController,而您应该为第二行调用 configureMailController2
我试图让 if indexPath.section == 1 && indexPath.row == 0
和 if 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)
}
}
您在两个按钮上调用了相同的方法。像这样更改您的代码。
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 = configureMailController2() //Your error was in this line
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)
}
}
您正在为两行调用 configureMailController,而您应该为第二行调用 configureMailController2