簡體   English   中英

應用關閉后不保存用戶默認設置

[英]Doesnt save userdefaults once the app closes

我創建了一個應用程序,它是一個uitableview,具有5個單元格[1, 2, 3, 4, 5]數組,可以在點擊該單元格時進行編輯。 我已經完成,當您點擊一個單元格時,它會顯示一個Alertview和一個文本字段。 您可以簡短地編輯單元格。 而且,當它轉到另一個視圖控制器時,我已經保存了編輯。 但是問題是當我嘗試關閉應用程序並打開它時,它返回到默認的消息數,例如[1、2、3、4、5]。 任何提示如何保存已編輯的單元格,但同時打開ableableview時,它會顯示數組。 對不起我的英語不好。 謝謝順便說一句,我是編程新手,對此代碼感到抱歉

* Messages.Swift =表格視圖

class Messages: UITableViewController {

    @IBOutlet var uitableView: UITableView!

    var tField: UITextField!
    var index: Int?

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func viewWillAppear(_ animated: Bool) {

        //Get the array
        if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> {
            self.app.helper.messages = array
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return app.helper.messages.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let messagesCell = app.helper.messages[indexPath.row]
        cell.textLabel?.text = messagesCell

        return cell

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let path = tableView.indexPathForSelectedRow
        index = path?.row

        changeMessage()
    }

    func changeMessage() {

        let alert = UIAlertController(title: "Message", message: "Enter new preset message", preferredStyle: UIAlertControllerStyle.alert)

        alert.addTextField(configurationHandler: configurationTextField)

        let doneAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler:{ (UIAlertAction) in

            let modelString = self.tField.text
            self.app.helper.messages[self.index!] = modelString!
            self.app.helper.saveToDefaults()
            self.tableView.reloadData()

        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

        alert.addAction(doneAction)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }

    func configurationTextField(textField: UITextField!){
        if (textField) != nil {
            tField = textField
            textField.text = app.helper.messages[index!]
        }
    }
}

* Helper.swift

class Helper {

var messages: [String] = ["1", "2", "3", "4", "5"]

    func saveToDefaults() {
        let defaults = UserDefaults.standard
        defaults.set(messages, forKey: "messages")
        defaults.synchronize()

    }
}

在設置表視圖委托之前,嘗試加載用戶默認值。 因為表視圖是使用您在助手中分配的數據集加載的,而不是使用用戶默認設置的數據集加載的。

override func viewDidLoad() {
    super.viewDidLoad()

    //Get the array
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> {
        self.app.helper.messages = array
    }

    tableView.delegate = self
    tableView.dataSource = self
}

或者您可以重新加載表格視圖,因為從defaut用戶加載數據集后,視圖將出現

override func viewWillAppear(_ animated: Bool) {

    //Get the array
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> {
        self.app.helper.messages = array
    }
    tableView.reloadData()
}

嘗試

override func viewWillAppear(_ animated: Bool) {

    //Get the array
    if let array = UserDefaults.standard.object(forKey:"messages") as? Array<String> {
        self.app.helper.messages = array
    }
    //make this array global(declare it below var index: Int?)
    tableView.reloadData()
}

let messagesCell = array[indexPath.row]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM