简体   繁体   English

如何将数据从两个 UITextViews 保存到 UserDefaults

[英]How to save data from two UITextViews to UserDefaults

For each UITextView using UserDefaults , I've made a function to save and a function to display.对于每个使用UserDefaultsUITextView ,我制作了一个 function 来保存和一个 function 来显示。

Whatever text is added needs to be displayed at the time of adding, saved and then displayed again when opening the app again.添加的任何text都需要在添加时显示,保存,然后在再次打开应用程序时再次显示。

If I install the app with ONLY the function to save, quit the app and add the function to display then reinstall without deleting the installed app everything works perfectly.如果我仅使用 function 安装应用程序以保存,退出应用程序并添加 function 以显示然后重新安装而不删除已安装的应用程序一切正常。

If I install the app with both functions added it doesn't work.如果我安装添加了这两个功能的应用程序,它就不起作用。

There has to be a simple solution for this, I'm obviously doing something wrong.必须有一个简单的解决方案,我显然做错了什么。

The data from one textView is used to calculate results and then to display them on the other textView .一个textView的数据用于计算结果,然后显示在另一个textView上。 All data is added with other functions, none by the user.所有数据都是用其他功能添加的,用户没有。

    numberHistoryView.isEditable = false
    numberHistoryView.isSelectable = false

func saveHistoryTextView()
{
    let defaults = UserDefaults.standard
    let numberHistory = numberHistoryView.text
    defaults.set(numberHistory, forKey: "combos")
}
func displaySavedHistory()
{
    let defaults = UserDefaults.standard
    let savedCombos = defaults.object(forKey: "combos") as? String ?? ""
    numberHistoryView.text = savedCombos
}

func saveFrequencyTextView()
{
    let defaults = UserDefaults.standard
    let numberFrequency = numberFrequencyCount.text
    defaults.set(numberFrequency, forKey: "frequency")
}
func displaySavedFrequency()
{
    let defaults = UserDefaults.standard
    let savedFrequency = defaults.object(forKey: "frequency") as? String ?? ""
    numberFrequencyCount.text = savedFrequency
}


override func viewWillDisappear(_ animated: Bool)
{
    saveHistoryTextView()
    saveFrequencyTextView()
}
override func viewWillAppear(_ animated: Bool)
{
    displaySavedHistory()
    displaySavedFrequency()
}

This depends on the order and timing in which you are calling save and display methods.这取决于您调用savedisplay方法的顺序和时间

  1. When you're installing a fresh app, there will be no data in saved in UserDefaults .当您安装新的应用程序时, UserDefaults中不会保存任何数据。 So when you call displaySavedHistory() and displaySavedFrequency() methods in viewWillAppear(_:) , nothing will be fetched because nothing is saved yet.因此,当您在viewWillAppear(_:)中调用displaySavedHistory()displaySavedFrequency()方法时,将不会获取任何内容,因为尚未保存任何内容。

  2. Now, when you save the data using saveHistoryTextView() and saveFrequencyTextView() methods in viewWillDisappear(_:) and then you kill and run the app again, the saved data will be fetched and displayed.现在,当您使用viewWillDisappear(_:)中的saveHistoryTextView()saveFrequencyTextView()方法保存数据,然后您杀死并再次运行应用程序时,将获取并显示保存的数据。

Also, since you're saving the data in UserDefaults , and UserDefaults are saved within the sandbox , so the data won't persist when you delete the app.此外,由于您将数据保存在UserDefaults中,并且UserDefaults保存在sandbox中,因此当您删除应用程序时数据不会保留。 You've to save the data in iCloud or keychain etc. if you want to persist the data even after app deletion.如果您想在应用程序删除后保留数据,您必须将数据保存在iCloudkeychain等中。

Once I put my brain into a theta state with the right frequency I managed to figure it out.一旦我以正确的频率将大脑放入θstate,我就设法弄清楚了。

Thanks to @Naresh and all other contributors for trying to help as you may have assisted me a little.感谢@Naresh 和所有其他贡献者的帮助,因为您可能对我有所帮助。

The solution basically just required a simple if statement.该解决方案基本上只需要一个简单的 if 语句。

Everything now works perfectly.现在一切正常。

func saveHistoryTextView()
{
    if numberHistoryView.text?.count != nil
    {
        let defaults = UserDefaults.standard
        defaults.set(numberHistoryView.text!, forKey: "combos")
    }
}
func displaySavedHistory()
{
    let defaults = UserDefaults.standard
    if let savedCombos = defaults.string(forKey: "combos")
    {
        numberHistoryView.text = savedCombos
    }
}

func saveFrequencyTextView()
{
    if numberFrequencyCount.text?.count != nil
    {
        let defaults = UserDefaults.standard
        defaults.set(numberFrequencyCount.text!, forKey: "frequency")
    }
}
func displaySavedFrequency()
{
    let defaults = UserDefaults.standard
    if let savedFrequency = defaults.string(forKey: "frequency")
    {
        numberFrequencyCount.text = savedFrequency
    }
  }
}


override func viewWillDisappear(_ animated: Bool)
{
    saveHistoryTextView()
    saveFrequencyTextView()
}
override func viewWillAppear(_ animated: Bool)
{
    displaySavedHistory()
    displaySavedFrequency()
}

You can do it with property observer as:您可以使用属性观察器来执行此操作:

private let DATA_KEY = "Saved Data"

//After initialising the outlet we can set the data
@IBOutlet weak var textView: UITextView! {
    didSet {
        textView.text = self.data
    }
}

private var data: String {
    set {
        //Save data in user defaults
        UserDefaults.standard.set("The value you will assign", forKey: DATA_KEY)
    }
    get {
        //get the data from user defaults.
        return UserDefaults.standard.value(forKey: DATA_KEY) as? String ?? ""
    }
}

//UITextViewDelegate: set the text data on end of UITextView editing
func textViewDidEndEditing(_ textView: UITextView) {
    self.data = textView.text
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM