gpt4 book ai didi

swift - 将选择器 View 返回到先前选择的行

转载 作者:行者123 更新时间:2023-11-30 10:22:18 25 4
gpt4 key购买 nike

在我的应用程序中,我允许用户通过选择器 View 更改应用程序的颜色主题。在此过程中,栏和按钮等 UI 元素会根据用户的选择进行更改。

当 View 关闭并重新打开时,选择器 View 返回到第一个项目时遇到问题。如何使选择器 View 返回用户之前选择的颜色?

这是我当前用于选择器 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
}
}

最佳答案

假设您的选择器中有一列(“组件”):

override func viewDidLoad() {
super.viewDidLoad()
let idx = NSUserDefaults.standardUserDefaults().integerForKey("SavedIndex")
pickerView.selectRow(idx, inComponent: 0, animated: false)
}

当然,当选择一行时,保存到 NSUserDefaults

根据您的设置,您可能需要将其放入 viewWillAppear() 中。

关于swift - 将选择器 View 返回到先前选择的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25333305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com