N3ur0n
N3ur0n

Reputation: 53

Swift UISegmentedControl IBAction values do not change

So I am wanting to keep track of which direction the user would like to cycle through. Hence I have a segmented control to select the direction.

Here is the code:

@IBOutlet weak var cycleSwitch: UISegmentedControl!
@IBAction func setCycleDirection(sender: AnyObject) {

        switch cycleSwitch.selectedSegmentIndex
            {
        case 0:
            cycleDirection = cycleSwitch.selectedSegmentIndex
        case 1:
            cycleDirection = cycleSwitch.selectedSegmentIndex
        default:
            break
        }
    println(cycleDirection)
}

but nothing gets printed. Even if I comment everything out and just have a println("Im in the action") does not log. I have also tried this as if statements e.g. cycleSwitch.selectedSegmentIndex == 0 etc. Neither works.

Any suggestions or thoughts as to why the values won't change?

Upvotes: 2

Views: 2413

Answers (1)

andrewcbancroft
andrewcbancroft

Reputation: 951

A few things to check:

  1. Make sure your segmented control is hooked up to the @IBAction via its "Value Changed" action. To do this, control+click your segmented control in your Storyboard and double-check to make sure "Value Changed" is wired to your View Controller
  2. Change the sender: argument to be a UISegmentedControl instead of AnyObject
  3. Instead of referencing your cycleSwitch @IBOutlet, reference sender in your switch statement:
switch sender.selectedSegmentIndex
                {
            case 0:
                cycleDirection = sender.selectedSegmentIndex
            case 1:
                cycleDirection = sender.selectedSegmentIndex
            default:
                break
            }

Upvotes: 5

Related Questions