Swift中的MFMailComposeViewController [英] MFMailComposeViewController in Swift
本文介绍了Swift中的MFMailComposeViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是示例代码:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func showEmail(sender : AnyObject) {
var emailTitle = "Test Email"
var messageBody = "This is a test email body"
var toRecipents = ["a.nakhimov@gmail.com"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result {
case MFMailComposeResultCancelled:
NSLog("Mail cancelled")
case MFMailComposeResultSaved:
NSLog("Mail saved")
case MFMailComposeResultSent:
NSLog("Mail sent")
case MFMailComposeResultFailed:
NSLog("Mail sent failure: %@", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
}
在函数mailComposeController中,每次都会出错 case 表达式:
In function mailComposeController I get an error on every case expression:
无法找到接受提供参数的重载'〜='。
我做错了什么?
推荐答案
我在Xcode 5和Xcode 6上比较了 MFMailComposeResult
文档。
在Swift中, MFMailComposeResult
是一个结构
I compared MFMailComposeResult
documentation on both Xcode 5 and Xcode 6.
In Swift, MFMailComposeResult
is a struct
struct MFMailComposeResult {
init(_ value: CUnsignedInt) // available in iPhone 3.0
var value: CUnsignedInt
}
与 MFMailComposeResultCancelled
作为类型的常量MFMailComposeResult
:
var MFMailComposeResultCancelled: MFMailComposeResult { get }
while它是Objective-C中的枚举:
while it's an enum in Objective-C:
enum MFMailComposeResult {
MFMailComposeResultCancelled,
MFMailComposeResultSaved,
MFMailComposeResultSent,
MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult; // available in iPhone 3.0
为了使您的代码有效,您必须比较它们的值这是 CUnsignedInt
。
In order to make your code work, you will have to compare their values which are CUnsignedInt
.
所以你必须输入以下代码:
So you will have to type the following code:
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: \(error.localizedDescription)")
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
这篇关于Swift中的MFMailComposeViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文