在我的应用中,我允许用户通过选择器视图更改应用的颜色主题。在这样做时,诸如条和按钮之类的UI元素基于用户选择的内容而改变。
当视图关闭并重新打开时,我遇到了选择器视图返回第一个项目的问题。如何使选择器视图返回到用户先前选择的颜色?
以下是我目前为选择器视图提供的代码:
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return coloursArray.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return coloursArray[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent: Int) {
colourSelected = row
if colourSelected == 0 {
self.navigationController.navigationBar.barTintColor = UIColor.newBlueColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newBlueColor()
UINavigationBar.appearance().barTintColor = UIColor.newBlueColor()
UISegmentedControl.appearance().tintColor = UIColor.newBlueColor()
changeButton.layer.backgroundColor = (UIColor.newBlueColor()).CGColor
} else if colourSelected == 1 {
self.navigationController.navigationBar.barTintColor = UIColor.newGreenColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newGreenColor()
UINavigationBar.appearance().barTintColor = UIColor.newGreenColor()
UIButton.appearance().setTitleColor(UIColor.newGreenColor(), forState: UIControlState())
UISegmentedControl.appearance().tintColor = UIColor.newGreenColor()
changeButton.layer.backgroundColor = (UIColor.newGreenColor()).CGColor
} else if colourSelected == 2 {
self.navigationController.navigationBar.barTintColor = UIColor.newOrangeColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newOrangeColor()
UINavigationBar.appearance().barTintColor = UIColor.newOrangeColor()
UISegmentedControl.appearance().tintColor = UIColor.newOrangeColor()
changeButton.layer.backgroundColor = (UIColor.newOrangeColor()).CGColor
} else if colourSelected == 3 {
self.navigationController.navigationBar.barTintColor = UIColor.newPurpleColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newPurpleColor()
UINavigationBar.appearance().barTintColor = UIColor.newPurpleColor()
UISegmentedControl.appearance().tintColor = UIColor.newPurpleColor()
changeButton.layer.backgroundColor = (UIColor.newPurpleColor()).CGColor
} else if colourSelected == 4 {
self.navigationController.navigationBar.barTintColor = UIColor.newRedColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newRedColor()
UINavigationBar.appearance().barTintColor = UIColor.newRedColor()
UISegmentedControl.appearance().tintColor = UIColor.newRedColor()
changeButton.layer.backgroundColor = (UIColor.newRedColor()).CGColor
} else if colourSelected == 5 {
self.navigationController.navigationBar.barTintColor = UIColor.newTurquoiseColor()
self.tabBarController.tabBar.selectedImageTintColor = UIColor.newTurquoiseColor()
UINavigationBar.appearance().barTintColor = UIColor.newTurquoiseColor()
UISegmentedControl.appearance().tintColor = UIColor.newTurquoiseColor()
changeButton.layer.backgroundColor = (UIColor.newTurquoiseColor()).CGColor
}
}
答案 0 :(得分:1)
假设您的选择器中有一列("组件"):
override func viewDidLoad() {
super.viewDidLoad()
let idx = NSUserDefaults.standardUserDefaults().integerForKey("SavedIndex")
pickerView.selectRow(idx, inComponent: 0, animated: false)
}
当然,选择行时保存到NSUserDefaults
。
根据您的设置,您可能需要将其放入viewWillAppear()
。