gpt4 book ai didi

ios - 使用 UserDefault 时电子邮件主题显示为 nil

转载 作者:行者123 更新时间:2023-11-28 12:22:39 25 4
gpt4 key购买 nike

我有 5 个 View Controller ,你在屏幕 2 上输入了你的名字,它应该传递它的数据,以便在屏幕 5,即电子邮件屏幕中,主题有你的名字。应该像:屏幕 2:“Bill”,屏幕 5:“姓名:‘Bill’”,但它显示的是“姓名:nil”。我使用 UserDefault 传递数据,这是我的屏幕 2 代码:

   @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 = ["thejoshytv@gmail.com"]

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(nil, forKey: "UserName")

您基本上是将值设置为 nil。

另一方面,UserDefauls 会定期保存到磁盘,因此通过在某个时候将其设置为 nil,它将保留在磁盘上(之前的值将被删除)。

https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

关于ios - 使用 UserDefault 时电子邮件主题显示为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44337908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com