Reputation: 11175
In my app I allow the user to change the colour theme for the app via a picker view. In doing this, UI elements such as the bars and buttons are changed based on what the user chooses.
I am having an issue with the picker view returning to the first item when the view is closed and reopened. How can I make the picker view return to the colour that the user previously selected?
Here is the code that I currently have for the picker view:
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
}
}
Upvotes: 0
Views: 149
Reputation: 80265
Supposing you have one column ("component") in your picker:
override func viewDidLoad() {
super.viewDidLoad()
let idx = NSUserDefaults.standardUserDefaults().integerForKey("SavedIndex")
pickerView.selectRow(idx, inComponent: 0, animated: false)
}
And, of course, save to NSUserDefaults
when a row has been selected.
Depending on your setup, you might have to put this into viewWillAppear()
.
Upvotes: 1