Chapter 4. why can not use slider (var) in sliderMoved?

why i can not use slider(var) in sliderMoved, instead of (_slider : UISlider)
but can use in startNewRound???

@ceolt It should actually work after all - what is the error you are getting at runtime?

yes, when i build my code above, then slide the slider…

It sounds as if you might not have connected your IBOutlet to the slider. Can you check if that is the case? If not, can you copy and paste the full error (especially the top) part that you see in the Xcode console?

i connected IBOutlet to slider.
My question is "Why i must write (_slider : UISlider) in sliderMoved??
And Why i can’t use IBOulet… slider for sliderMoved, instead of assign new variable (_slider) "???
Why??
@IBAction func sliderMoved() {
currentValue = lroundf(slider.value)
print(“The value of the slider is now : (currentValue)”)
} // ERROR // use IBOutlet slider for sliderMoved()…

Here is my console :

Your error/crash is because you changed the signature of the sliderMoved method. I think you originally had the method as @IBAction func sliderMoved(_ slider:UISlider) and then later changed it to @IBAction func sliderMoved() .

Those are treated as two different methods by the compiler since one has a parameter and the other does not. Since you connected the outlet to the original method, you would need to disconnect it and then re-connect it for the right method to be hooked up.

Does that part make sense?

As far as your question as to why you must write the method as @IBAction func sliderMoved(_ slider:UISlider) , that depends on what you are trying to do. You should be able to write the method without the parameter, but as I said, you need to hook up your method again in that case if you originally had the method with the parameter.

Hope this helps :slight_smile:

Show me how i can write @IBAction func sliderMoved(). instead of @IBAction func sliderMoved(_ slider:UISlider), but compile is fine, and build success…
Why i must write @IBAction func sliderMoved(_ slider:UISlider), instead of @IBAction func sliderMoved()..
if i just write @IBAction func sliderMoved(), and use slider: IBOutlet, like starOver() used.
Why can not use IBOutlet slider in sliderMoved(), but starOver() can???

I have already answered your question. You can write the method as @IBAction func sliderMoved() as I mentioned above. It will compile fine. I just tried it and it works fine here.

If you cannot get it to compile with the method name that way, please provide a screenshot of the compiler error at that point showing the code you’re using.

This topic was automatically closed after 166 days. New replies are no longer allowed.