向NSUserDefaults添加内容 - 多个视图

时间:2015-09-12 22:38:54

标签: ios arrays swift nsuserdefaults

我正在创建一个类似字典的应用程序,我想使用NSUserDefaults存储用户的搜索历史记录。我在课外创建了一个seaechHistory数组。现在我追加searchHistory数组并使用willSelectRowAtIndexPath中的NSUserDefaults保存它,并在历史记录视图中检索存储的历史记录。存储工作正常,但是当我重新运行应用程序时,NSUserDefaults将seachHistory设置为空数组,因此我丢失了所有已保存的数据。 synchronize()方法可以在任何地方使用。

初始视图控制器:

 import UIKit

var searchHistory = [String]()


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {


    @IBOutlet weak var tableView: UITableView!
    @IBOutlet var textField: UITextField!

    @IBAction func textFieldDidChange(sender: AnyObject) {
        tableView.hidden = false
        searchManager.updateFilter(textField.text)
        tableView.reloadData()
    }

    @IBAction func tapOnTextField(sender: AnyObject) {

        let textFieldLength = count(textField.text)

        if textFieldLength == 0 {
            tableView.hidden = true

        } else {
        tableView.hidden = false
        }
    }


    override func viewDidLoad() {

        super.viewDidLoad()

        textField.backgroundColor = UIColor.whiteColor()

        tableView.hidden = true

        self.textField.delegate = self

    }


    var searchManager = SearchManager()


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchManager.filteredURLCount()
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

        cell.textLabel!.text = searchManager.filteredURLAtIndex(indexPath.row)

        return cell
    }


    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        self.textField.text = searchManager.filteredURLAtIndex(indexPath.row)

        searchHistory.append(textField.text)

        NSUserDefaults.standardUserDefaults().setObject(searchHistory, forKey: "History")

        self.performSegueWithIdentifier("cellTapped", sender: "Cell")

        textField.text = nil
        tableView.hidden = true
        textField.resignFirstResponder()

        return indexPath
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
        self.tableView.hidden = true
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "cellTapped" {
        var DestViewController: definitionView = segue.destinationViewController as! definitionView
        DestViewController.searchWord = textField.text
        } 
    }


}

历史记录视图控制器:

    import UIKit

class historyView: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        searchHistory  = NSUserDefaults.standardUserDefaults().objectForKey("History")! as! [String]
        println(searchHistory)

    }

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


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var destination: ViewController = segue.destinationViewController as! ViewController

    }

}

1 个答案:

答案 0 :(得分:0)

您可以在viewDidLoad()方法中读取数组,然后它不会为空。

在ViewController中添加代码:

override func viewDidLoad() {
    super.viewDidLoad()
    searchHistory = NSUserDefaults.standardUserDefaults().objectForKey("History")! as! [String]
}

它应该有用。

相关问题