I have 2 x UITextFields for the user to put values in. I want to use those values as the min & max values for the UISlider. I have been struggling with this for 2 days already.
This is my current code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sensorMinText: UITextField!
@IBOutlet weak var sensorMaxText: UITextField!
@IBOutlet weak var sliderLabel: UILabel!
var sensorMin: String?
var sensorMax: String?
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
Slider.value = 0
Slider.minimumValue =
Slider.maximumValue =
sliderLabel.text = String(Int(Slider.value))
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
@IBAction func sMinInput(_ sender: Any) {
sensorMin = sensorMinText.text
}
@IBAction func sMaxInput(_ sender: Any) {
sensorMax = sensorMaxText.text
}
@IBOutlet weak var Slider: UISlider!
@IBAction func sliderChanged(_ sender: UISlider) {
sliderLabel.text = String(Int(sender.value))
}
}
I managed to get the App working as intended.
Create a func above viewDidLoad
that changes the Slider's minimumValue
and maximumValue
properties to the user's inputs i from minTextField
and maxTextField
.
Call the func in the minimum (sMinInput
) and maximum (sMaxInput
) input senders respectively.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sensorMinText: UITextField!
@IBOutlet weak var sensorMaxText: UITextField!
@IBOutlet weak var sliderLabel: UILabel!
//Below create func to set Slider's values to user's inputs.
func changeSlider() {
Slider.minimumValue = Float(sensorMinText.text!) ?? 0.0
Slider.maximumValue = Float(sensorMaxText.text!) ?? 0.0
}
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action:
#selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
sliderLabel.text = String(Int(Slider.value))
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
//Call func to update Slider values when user inputs min value
@IBAction func sMinInput(_ sender: Any) {
changeSlider()
}
//Call func to update Slider values when user inputs max value
@IBAction func sMaxInput(_ sender: Any) {
changeSlider()
}
@IBOutlet weak var Slider: UISlider!
@IBAction func sliderChanged(_ sender: UISlider) {
sliderLabel.text = String(Int(sender.value))
}
}