无法从下拉菜单中添加用户默认值

时间:2019-05-02 14:10:32

标签: arrays swift

我有一个ViewController,其主页上有一个滑出的汉堡菜单。菜单上有一个名为“激励”的选项,单击该选项会弹出一个Alert,提示用户将项目添加为其默认值。当您单击指定的ViewController时,这些默认值将在UIPickerView的主页上可用。

一切似乎都可以正常工作,除了添加该项目后,它不会与数组中已有的其他项目一起出现在UIPickerView中。

这是初始化默认值的方式。

let defaults = UserDefaults.standard

这是viewDidLoad()

中的数组
needIndicatorArray = defaults.object(forKey: "needs") as? [String] ?? [String]()

这是在程序主体中按下按钮时调用的代码。


@IBAction func addIncentiveButtonPressed(_ sender: UIButton) {

        var textField = UITextField()

        let alert = UIAlertController(title: "Add Need Indicator", message: "", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Add item", style: .default, handler: { (action) in
            self.needIndicatorArray.append(textField.text!)
            self.defaults.set(self.needIndicatorArray, forKey: "needs")
        }))
        alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
        }))
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new item"
            textField = alertTextField
        }
        present(alert, animated: true, completion: nil)
    }

I am expecting the items added by the ```Alerts``` in the hamburger menu to be available when the array loads.

0 个答案:

没有答案
相关问题