链接两个UISegmentedControls

问题描述 投票:0回答:2

我目前正在尝试连接两个UISegmentedControls,其中第一个Viewcontroller连接到第二个。点击第二个UISegment将更新第一个UISegment的值。

视图控制器:

class ViewController: UIViewController {
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var totalLabel: UILabel!
    @IBOutlet weak var billField: UITextField!
    @IBOutlet weak var tipController: UISegmentedControl!
    @IBOutlet weak var splitController: UISegmentedControl!
    @IBOutlet weak var splitLabel: UILabel!
    let defaults = UserDefaults.standard

    override func viewDidLoad() {

        super.viewDidLoad()
       defaults.synchronize()
        billField.becomeFirstResponder()
       tipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index")
        print(tipController.selectedSegmentIndex)
        // Do any additional setup after loading the view, typically from a nib.

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


//    @IBAction func toSettings(_ sender: Any) {
//        self.performSegue(withIdentifier: "toSettings", sender: self)
//    }

    @IBAction func onTap(_ sender: Any) {
        view.endEditing(true)
    }
    @IBAction func calculateTip(_ sender: Any) {

        let tipPercentages = [0.18, 0.20, 0.25]
        let splitNumbers = [1,2,3,4]
        let bill = Double(billField.text!) ?? 0
        let tip = bill * tipPercentages[tipController.selectedSegmentIndex]
        let total = bill + tip

        tipLabel.text = String(format: "$%.2f", tip)
        totalLabel.text = String(format: "$%.2f", total)
        splitLabel.text = String(format: "$%.2f", total/Double((splitNumbers[splitController.selectedSegmentIndex])))

}

SettingViewController:

class SettingsViewController: UIViewController {
     var tipPercentageIndex:Int!
    @IBOutlet weak var settingsTipController: UISegmentedControl!
    let defaults = UserDefaults.standard

    override func viewDidLoad() {
        super.viewDidLoad()
        settingsTipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index")

        // Do any additional setup after loading the view.
    }


    @IBAction func Index(_ sender: Any) {
      tipPercentageIndex = settingsTipController.selectedSegmentIndex
    }


    @IBAction func saveButton(_ sender: Any) {
        tipPercentageIndex = settingsTipController.selectedSegmentIndex
        defaults.set(tipPercentageIndex, forKey: "default_tip_index")
        defaults.synchronize()
        self.performSegue(withIdentifier: "Tippy", sender: self)
    }

    @IBAction func Back(_ sender: Any) {
       self.performSegue(withIdentifier: "Tippy2", sender: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

但是,我的解决方案不会更新值,也不会在从第二个切换回第一个时存储值。

注意:第一个是第二个。

ios swift uisegmentedcontrol
2个回答
0
投票

为分段控件添加Value Changed的IBAction,并更新受影响控件的值:

func updateSegments(){

    if let value = UserDefaults.standard.value(forKey: "key"){
        let selectedIndex = value as! Int
        yourSegmentedControl.selectedSegmentIndex = selectedIndex
    } else {
        yourSegmentedControl.selectedSegmentIndex = 0 // set to default
    }

    if let value = UserDefaults.standard.value(forKey: "anotherkey"){
        let selectedIndex = value as! Int
        anotherSegmentedControl.selectedSegmentIndex = selectedIndex
    } else {
        anotherSegmentedControl.selectedSegmentIndex = 0 // set to default
    }

   //etc...

}


@IBAction func yourSegmentedControlSelected(_ sender: UISegmentedControl) {
    UserDefaults.standard.set(sender.selectedSegmentIndex, forKey: "key")
    self.updateSegments()
}

@IBAction func anotherSegmentedControlSelected(_ sender: UISegmentedControl) {
    UserDefaults.standard.set(sender.selectedSegmentIndex, forKey: "anotherkey")
    self.updateSegments()
}

0
投票

从您解释代码的方式来看,我认为您的问题是加载依赖值的代码仅在加载viewController时调用。而且我相信,你正在“切换回”的viewController已经被加载了。因此,在更新默认值后,将不会再次执行设置代码。

一个快速解决方法是将viewDidLoad的设置代码移动到viewWillAppear

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    defaults.synchronize()
    billField.becomeFirstResponder()
    tipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index")
    print(tipController.selectedSegmentIndex)
}

更新刚刚注意到你说价值没有存储,以及我以前的建议试试这个 -

创建一个将默认值设置为settingsTipController.selectedSegmentIndex的方法。加载SettingsViewController时,将方法作为目标添加到settingsTipController上,该.valueChanged作用于override func viewDidLoad() { super.viewDidLoad() settingsTipController.selectedSegmentIndex = defaults.integer(forKey: "default_tip_index") settingsTipController.addTarget(nil, action: #selector(didToggleSwitch), for: .valueChanged) } @objc func didToggleSwitch() { tipPercentageIndex = settingsTipController.selectedSegmentIndex defaults.set(tipPercentageIndex, forKey: "default_tip_index") } @IBAction func saveButton(_ sender: Any) { tipPercentageIndex = settingsTipController.selectedSegmentIndex defaults.set(tipPercentageIndex, forKey: "default_tip_index") defaults.synchronize() self.performSegue(withIdentifier: "Tippy", sender: self) } 事件。点击保存时,将保存最后选择的值。

在SettingsViewController.swift中:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.