I have a function:
func handleChange(isShown:Bool){
if isShown {
// do something A
}else{
// do something B
}
}
If I want a UISlider to add the above action with the isShown defaulting to false, and I will call the method handleChange(isShown:Bool)
with true in another function.
The wrong code as follows:
slider.addTarget(self, action: #selector(handleValue(isShown:false)), for: .valueChanged)
And I know the right code needs to be:
slider.addTarget(self, action: #selector(handleValue(isShown:)), for: .valueChanged)
I just want to know how can I set the 'isShown' as false in swift? Appreciate you can reply.
One solution is to use a helper function that is separate from the selector
function. The selector function can call the helper function, which can have as many parameters as you wish.
Adding the target:
slider.addTarget(self, action: #selector(handleChange(_ sender: UISlider)), for: .valueChanged)
Target function:
func handleChange(sender: UISlider) {
// logic based on the action or slider
doSomething(isShown: true)
}
Helper function:
func doSomething(isShown: Bool) {
// make your changes using the parameters here
}