Bird
0
0

Identify the error in this Swift code for UISlider:

medium📝 Debug Q14 of 15
iOS Swift - User Input and Forms
Identify the error in this Swift code for UISlider:
let slider = UISlider()
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 50
slider.addTarget(self, action: #selector(sliderChanged), for: .valueChanged)

func sliderChanged() {
    print("Slider moved")
}
AUISlider cannot be created without a frame
BThe sliderChanged method must have a parameter for the sender
CminimumValue and maximumValue must be integers
DaddTarget method is missing the event type
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature for target-action

    The action method for UISlider must accept the sender parameter (usually UISlider) to match the selector signature.
  2. Step 2: Verify other parts

    minimumValue and maximumValue are Float, so integers are allowed as they convert. UISlider can be created without frame in code. addTarget includes event type .valueChanged correctly.
  3. Final Answer:

    The sliderChanged method must have a parameter for the sender -> Option B
  4. Quick Check:

    Action method needs sender parameter [OK]
Quick Trick: Action methods need sender parameter for UIControl events [OK]
Common Mistakes:
  • Omitting sender parameter in action method
  • Thinking min/max must be Int instead of Float
  • Assuming UISlider requires frame on creation
  • Missing event type in addTarget

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes