0
我有5个视图控制器,并且您在屏幕2上输入了您的姓名,并且它应该传递其数据,以便在屏幕5中的电子邮件屏幕中,主题具有您的姓名。应该是这样的:屏幕2:“Bill”,屏幕5:“Name:'Bill'”,而是显示“Name:nil”。使用UserDefault来传递数据, 这里林是我的屏幕2代码:使用UserDefault时电子邮件主题显示为零
@IBAction func extButton(_ sender: Any) {
self.performSegue(withIdentifier: "whotoclass", sender: self)
}
func textFieldDidEndEditing(_ textField: UITextField) {
UserDefaults.standard.setValue("your username", forKey: "UserName")
UserDefaults.standard.synchronize()
UserDefaults.standard.setValue(nil, forKey: "UserName")
}
和屏幕5:
let strUserName = UserDefaults.standard.value(forKey: "UserName")
var myString = String()
@IBOutlet var pickerview2: UIPickerView!
@IBOutlet var label: UILabel!
let reason = ["Busy - General","Filming","Editing","Computer Task","Helping","Getting Set Up"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return reason[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return reason.count
}
@IBAction func sendButton(_ sender: Any) {
let toRecipients = ["[email protected]"]
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setToRecipients(toRecipients)
mc.setSubject("Media Team Task")
mc.setMessageBody("Hi There, A Notice to tell you that \(strUserName) will not be in class today.", isHTML: false)
present(mc, animated: true, completion: nil)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil);
self.performSegue(withIdentifier: "reasontosent", sender: self)
}
'UserDefaults.standard.setValue(零,forKey: “用户名”)'你为什么要这么做? –