0
votes

I have a View controller that has a tableView embedded in a ContainerView and just below the ContainerView I have a UISlider. I have code on a view controller for the UISlider and code on another view controller that controls the table view.

Properties of the UISlider are set based on the selected text field - this section of code works. I am struggling to create a function/feature that will change the textField value when the UISlider is move. I think the UISlider Action needs to on the code that controls the UISlider, but I cannot determine how to cast the value of the UISlider.setvalue between the two viewController as the slider is moved to update the textField located in a tableCell. Hopefully makes some sense.

   // UISlider ViewController

    override func viewDidLoad() {
    super.viewDidLoad()

    sliderOutlet.isContinuous = true
    sliderOutlet.tintColor = UIColor.green
    self.refreshSlider()

}

@objc func refreshSlider() {
    sliderOutlet.minimumValue = Float(GlobalSliderValues.minimumValue)
    sliderOutlet.maximumValue = Float(GlobalSliderValues.maximumValue)
    sliderOutlet.value = Float(GlobalSliderValues.sliderValue)


//        if let chgIntValue = Int(GlobalSliderValues.changeValue)
//        { sliderOutlet.setValue(Float(Double(chgIntValue)), animated: true)
//        }

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(self.refreshSlider), name: Notification.Name("refreshSlider"), object: nil)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: Notification.Name("refreshSlider"), object: nil)
}

TableView Controller

 override func viewDidLoad() {

    super.viewDidLoad()
mortgageAmount.addTarget(self, action: #selector(chgTextFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
}

@objc func chgTextFieldDidChange(textField: UITextField)
{

    if let chgStringValue = mortgageAmount.text
    {
        if Double(chgStringValue) ?? 1 > 10000 {
            let alert = UIAlertController(title: "Input Error", message: "Value cannot be greater than 10000", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))

            self.present(alert, animated: true, completion: nil)
            return
        }

        GlobalSliderValues.minimumValue = 10
        GlobalSliderValues.maximumValue = 10000
        GlobalSliderValues.sliderValue = Int(mortgageAmount.text!)!
        GlobalSliderValues.mortageAmountValue = Float(Int(mortgageAmount.text!)!)

        NotificationCenter.default.post(name:Notification.Name("refreshSlider"), object: nil)

        if let chgIntValue = Int(chgStringValue)
        { GlobalSliderValues.changeValue.setValue(Float(Double(chgIntValue)), animated: true)
        }
    }
}
@IBAction func valueChanged(_ sender: UISlider) {

    mortgageAmount.text = String(format: "%.2f",Double(sender.value))
}

   struct GlobalSliderValues {

        static var minimumValue = Int()
        static var maximumValue = Int()
        static var lowerValue = Int()
        static var UpperValue = Int()
        static var locationValue = Int()
        static var sliderValue = Int()
        static var sliderChgValue = ""


    }
1

1 Answers

0
votes

We don't need notification center to deal with refreshing the slider. Since we are using constants to store value your code can be changed as follows

class ViewController: UIViewController {
    @IBOutlet weak var sliderOutlet: UISlider!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        sliderOutlet.isContinuous = true
        sliderOutlet.tintColor = UIColor.green

    }

    @objc func refreshSlider() {
        DispatchQueue.main.async {
            self.sliderOutlet.minimumValue = Float(GlobalSliderValues.minimumValue)
            self.sliderOutlet.maximumValue = Float(GlobalSliderValues.maximumValue)
            self.sliderOutlet.value = Float(GlobalSliderValues.sliderValue)
            print(self.sliderOutlet.minimumValue)
            print(self.sliderOutlet.maximumValue)
            print(self.sliderOutlet.value)
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.refreshSlider()
    }

}

I entered mortgage amount as so 1098 so slider look like below

enter image description here