使用UISlider的VC在VC2上更改文本字段

时间:2018-01-11 22:10:37

标签: ios swift uitableview uislider

我有一个View控制器,它有一个嵌入在ContainerView中的tableView,而在ContainerView下面我有一个UISlider。我在UISlider的视图控制器上有代码,在另一个控制表视图的视图控制器上有代码。

UISlider的属性是根据所选的文本字段设置的 - 这部分代码可以正常工作。我正在努力创建一个函数/功能,它将在UISlider移动时更改textField值。我认为UISlider Action需要控制UISlider的代码,但我无法确定如何在移动滑块以更新位于tableCell中的textField时,在两个viewController之间强制转换UISlider.setvalue的值。希望有道理。

   // 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控制器

 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 个答案:

答案 0 :(得分:0)

我们不需要通知中心来处理刷新滑块。由于我们使用常量来存储值,因此可以按如下方式更改代码

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

}

我输入按揭金额为1098,所以滑块看起来如下

enter image description here