链接两个 UISegmentedControls [英] Linking two UISegmentedControls

查看:22
本文介绍了链接两个 UISegmentedControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to connect two UISegmentedControls where the first Viewcontroller is connected to the second. Tapping the second UISegment will update the value of the first one.

视图控制器:

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

}

设置视图控制器:

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

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

However, my solution does not update the value nor store the value when switching back from the second to the first.

注意:第一个转入第二个.

Note: the first segues to the second.

推荐答案

从您对代码的解释来看,我认为您的问题是加载依赖值的代码仅在加载 viewController 时调用.而且我相信,您要切换回"的 viewController 已经加载.所以设置代码在更新你的默认值后不会再次执行.

From how you've explained your code, I think your problem is that the code that loads your dependent values, is only called when your viewController is loaded. And I believe, the viewController you are "switching back" to, has already been loaded. So the set up code will not be executed again after updating your defaults.

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

One quick fix to try is moving your set up code for viewDidLoad to viewWillAppear:

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

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

更新刚刚注意到你说值没有被存储,连同我之前的建议试试这个——

Update Just noticed you said the value is not stored, along with my pervious suggestion try this—

创建一个将默认值设置为 settingsTipController.selectedSegmentIndex 的方法.当您加载 SettingsViewController 时,将该方法作为目标添加到作用于 .valueChanged 事件的 settingsTipController.当点击保存时,将保存最后选择的值.

Create a method that sets the defaults to the settingsTipController.selectedSegmentIndex. When you load the SettingsViewController, add the method as a target to the settingsTipController that acts on a .valueChanged event. When save is tapped the last selected value will be saved.

在 SettingsViewController.swift 中:

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

这篇关于链接两个 UISegmentedControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆