链接两个UISegmentedControl

时间:2018-01-01 07:42:58

标签: ios swift uisegmentedcontrol

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

的ViewController:

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.
    }

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

注意:第一个是第二个。

2 个答案:

答案 0 :(得分: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。 点击保存时,将保存最后选择的值。

在SettingsViewController.swift中:

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)
}

答案 1 :(得分: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()
}