将值保存到 UserDefaults [重复]

Posted

技术标签:

【中文标题】将值保存到 UserDefaults [重复]【英文标题】:Save value to UserDefaults [duplicate] 【发布时间】:2020-11-20 22:12:46 【问题描述】:

我刚开始用 swift 编程,所以请考虑到这一点。我正在开发一个根据单击的按钮发送短信的应用程序。我已经弄清楚了一切,并且该应用程序运行良好,但是我遇到一个问题,即每次打开该应用程序时该应用程序都会提示我输入收件人号码。我发现电话号码没有保存,但我找不到将 usrTextField var 保存到 UserDefaults 的方法。这是应用程序的代码:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate
var usrTextField: UITextField?
var pwdTextField: UITextField?


override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
        

override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


    
@IBAction func displayAlertAction(_ sender: Any) 
    
    let alertController = UIAlertController(title: "Enter phone number and password", message: nil, preferredStyle: .alert)
    alertController.addTextField(configurationHandler: usrTextField)
    alertController.addTextField(configurationHandler: pwdTextField)
    
    let okAction = UIAlertAction(title: "OK", style: .default, handler: self.okHandler)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)
    
    self.present(alertController, animated: true)

    
    


func usrTextField(textField: UITextField!) 
    usrTextField = textField
    usrTextField?.placeholder = "06XXXXXXX"
    
    


    
func pwdTextField(textField: UITextField!) 
    pwdTextField = textField
    pwdTextField?.placeholder = "123456"
    pwdTextField?.isSecureTextEntry = true
    
            

func okHandler(alert: UIAlertAction!) 
    let simpleVC = SimpleVC()
    simpleVC.customInit(usrStr: (usrTextField?.text)!, pwdStr: (pwdTextField?.text)!)
    self.navigationController?.pushViewController(simpleVC, animated: true)


func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) 
    self.dismiss(animated: true, completion: nil)

@IBAction func Dugme1(_ sender: AnyObject)

    
    if MFMessageComposeViewController.canSendText()
    
        let controller = MFMessageComposeViewController ()
        controller.body = "Message1"
        if let temp = usrTextField 
           
            controller.recipients = [self.usrTextField!.text!]
            
         else 
            let alert = UIAlertController(title: "AlfaPro Alarm", message: "Please enter the phone number in settings", preferredStyle: .alert)

            alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
            self.present(alert, animated: true)
        
        
        controller.messageComposeDelegate = self
        self.present(controller, animated: true, completion: nil)
        let impact = UIImpactFeedbackGenerator() // 1
        impact.impactOccurred()
         
    
    

    else
    
        print("error in sending")
    




    

另一个故事板的代码:

import UIKit

class SimpleVC: UIViewController 
@IBOutlet weak var usrLabel: UILabel!
@IBOutlet weak var pwdLabel: UILabel!

var usrStr: String?
var pwdStr: String?


override func viewDidLoad() 
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    usrLabel.text = usrStr
    pwdLabel.text = pwdStr



func customInit(usrStr: String, pwdStr: String) 
    self.usrStr = usrStr
    self.pwdStr = pwdStr


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.

*/

【问题讨论】:

【参考方案1】:

在我的SceneDelegate.swift 底部,我有以下代码:

struct savedString 
    var string: String = UserDefaults.standard.string(forKey: "mystring") ?? "" 
        didSet 
            UserDefaults.standard.set(string, forKey: "mystring")
        
    

从那里,我可以使用将其加载到视图中

@State var myString = savedString()

然后在我的视图主体中使用它(例如设置占位符)

Text(myString.string) // get
TextField("My String", text: $myString.string) // set

【讨论】:

以上是关于将值保存到 UserDefaults [重复]的主要内容,如果未能解决你的问题,请参考以下文章

将值附加到数据数组并保存在 Userdefault swift

从特定的核心数据表行中检索值 - 然后将值存储到 UserDefaults

如何在 UserDefaults 中重置“AppleLanguages”键的值

尝试将数组保存到 UserDefaults

将结构保存到 UserDefaults

将结构保存到 UserDefaults