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: 「用戶名」)'你爲什麼要這麼做? –