You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift5中保存UISwitch状态并联动切换PickerView数据源

解决方案

一、保存UISwitch状态到本地

用UserDefaults实现轻量状态持久化,步骤如下:

  1. 定义状态存储键名
    在ViewController中添加常量统一管理键名:
private let fullStopSwitchKey = "FullStopSwitchState"
private let halfStopSwitchKey = "HalfStopSwitchState"
private let thirdStopSwitchKey = "ThirdStopSwitchState"
  1. 返回按钮点击时保存状态
    在返回按钮的点击方法中写入状态:
@IBAction func backButtonTapped(_ sender: UIButton) {
    UserDefaults.standard.set(fullStopSwitch.isOn, forKey: fullStopSwitchKey)
    UserDefaults.standard.set(halfStopSwitch.isOn, forKey: halfStopSwitchKey)
    UserDefaults.standard.set(thirdStopSwitch.isOn, forKey: thirdStopSwitchKey)
    // 执行返回逻辑,比如:navigationController?.popViewController(animated: true)
}
  1. 页面加载时恢复状态
    在viewDidLoad中读取并恢复状态,同时保持单选互斥规则:
override func viewDidLoad() {
    super.viewDidLoad()
    // 读取存储的状态
    let isFullStopOn = UserDefaults.standard.bool(forKey: fullStopSwitchKey)
    let isHalfStopOn = UserDefaults.standard.bool(forKey: halfStopSwitchKey)
    let isThirdStopOn = UserDefaults.standard.bool(forKey: thirdStopSwitchKey)
    
    // 恢复状态,首次启动默认选中1档
    fullStopSwitch.isOn = isFullStopOn || (!isHalfStopOn && !isThirdStopOn)
    halfStopSwitch.isOn = isHalfStopOn
    thirdStopSwitch.isOn = isThirdStopOn
    
    // 初始化当前光圈数组
    updateCurrentApertureArray()
}

二、实现PickerView多数组切换

核心是维护当前活跃的光圈数组,开关切换时更新数组并刷新PickerView:

  1. 定义光圈数组和当前数组变量
    在ViewController中添加:
// 三个档位对应的光圈数组,根据实际需求调整数据
private let fullStopApertures = ["f/1.0", "f/1.4", "f/2.0", "f/2.8", "f/4.0", "f/5.6", "f/8.0"]
private let halfStopApertures = ["f/1.0", "f/1.2", "f/1.4", "f/1.7", "f/2.0", "f/2.4", "f/2.8"]
private let thirdStopApertures = ["f/1.0", "f/1.1", "f/1.2", "f/1.4", "f/1.6", "f/1.8", "f/2.0"]

// 当前使用的光圈数组
private var currentApertures: [String] = []
  1. 添加开关状态变化处理
    先写统一更新数组的方法:
private func updateCurrentApertureArray() {
    if fullStopSwitch.isOn {
        currentApertures = fullStopApertures
    } else if halfStopSwitch.isOn {
        currentApertures = halfStopApertures
    } else if thirdStopSwitch.isOn {
        currentApertures = thirdStopApertures
    }
    // 刷新PickerView
    yourPickerView.reloadAllComponents()
}

给每个UISwitch的valueChanged事件绑定该方法(确保开关已连线IBAction):

@IBAction func stopSwitchChanged(_ sender: UISwitch) {
    // 依赖你已实现的单选互斥逻辑,直接更新数组即可
    updateCurrentApertureArray()
}
  1. 修改PickerView数据源方法
    确保数据源方法基于currentApertures返回数据:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return currentApertures.count
}

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

注意事项

  • 确保UISwitch的valueChanged事件已正确连线,且单选互斥逻辑在触发该事件前完成(比如选中一个开关时自动关闭另外两个)。
  • 如果光圈数组是数字类型(如CGFloat),只需调整数组类型和titleForRow的返回格式,例如:String(format: "f/%.1f", currentApertures[row])。

内容的提问来源于stack exchange,提问作者Wolfgang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:26:04