Swift 1.2 - 根据NSUserDefaults值

时间:2015-09-19 10:22:39

标签: ios swift uipickerview

编辑:要明确,这不是重复。 我的问题不是保存/重新搜索数据,而是显示正确的UIPickerView的行,在Swift 1.2中不再需要同步(如堆栈溢出中的许多答案所述)

在我的应用程序中,我使用pickerViewv来设置地理定位数据应该出现的半径。 我在NSUserDefaults中保存了该半径值。 在我看来,我成功保存了这样的设置(由printl检查),但我想更新UIPicker,因此用户可以了解当前的设置。此时,选择器总是默认显示5(即使key的值是另一个)。

编辑2:这解决了我的问题,多亏了答案。在viewWillAppear

switch kRadiusMessages {
        case 5.0:
            self.pickerView.selectRow(0, inComponent: 0, animated: true)
        case 10.0:
            self.pickerView.selectRow(1, inComponent: 0, animated: true)
        case 15.0:
            self.pickerView.selectRow(2, inComponent: 0, animated: true)
        case 20.0:
            self.pickerView.selectRow(3, inComponent: 0, animated: true)
        default:
            self.pickerView.selectRow(0, inComponent: 0, animated: true)
        }

这是原始的起始代码:

   import UIKit

class SingleSettingsTVC: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    var pickerDataSource = ["5 km", "10 km", "15 km", "20 km"]

    let defaults = NSUserDefaults.standardUserDefaults()
    var kRadiusMessages : Double!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.pickerView.dataSource = self
        self.pickerView.delegate = self

        kRadiusMessages = defaults.doubleForKey("radiusForMessages")
        println("SingleSettingsTVC kRadiusMessages is \(kRadiusMessages)")


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBOutlet weak var pickerView: UIPickerView!


    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerDataSource.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerDataSource[row]
    }




    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if(row == 0)
        {
            self.view.backgroundColor = UIColor.whiteColor()
            defaults.setDouble(5.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")

        }
        else if(row == 1)
        {
            self.view.backgroundColor = UIColor.redColor()
            defaults.setDouble(10.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else if(row == 2)
        {
            self.view.backgroundColor =  UIColor.greenColor()
            defaults.setDouble(15.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else if(row == 3)
        {
            self.view.backgroundColor =  UIColor.greenColor()
            defaults.setDouble(20.0, forKey: "radiusForMessages")
            self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
            println("value for kRadiusMessages is : *\(kRadiusMessages)*")
        }
        else
        {
            self.view.backgroundColor = UIColor.blueColor()
        }
    }

}

1 个答案:

答案 0 :(得分:0)

NSUserDefaults中设置值后,您需要同步它。 保存数据后,请输入以下行,然后尝试检索。

if(row == 0)
{
     self.view.backgroundColor = UIColor.whiteColor()
     defaults.setDouble(5.0, forKey: "radiusForMessages")
     defaults.synchronize()
     self.kRadiusMessages = defaults.doubleForKey("radiusForMessages")
      println("value for kRadiusMessages is : *\(kRadiusMessages)*")
}

已编辑:您可以使用以下行

设置初始行
self.pickerView.selectRow(0, inComponent: 0, animated: true)

希望这有帮助!