Swift UISegmentedControl IBAction值不会改变

时间:2014-09-30 17:35:27

标签: swift uisegmentedcontrol

所以我想跟踪用户想要循环的方向。因此,我有一个分段控件来选择方向。

以下是代码:

@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)
}

但没有打印出来。即使我评论所有内容并且只是有一个println(“我在行动中”)不记录。我也试过这个,好像是陈述,例如cycleSwitch.selectedSegmentIndex == 0等。两者都不起作用。

关于为什么值不会改变的任何建议或想法?

1 个答案:

答案 0 :(得分:5)

要检查的一些事项:

  1. 确保您的分段控件通过其“值已更改”操作与@IBAction连接。 要执行此操作,请在Storyboard中按住Ctrl并单击您的分段控件,然后仔细检查以确保“Value Changed”连接到View Controller
  2. sender:参数更改为UISegmentedControl而不是AnyObject
  3. 不是引用您的cycleSwitch @IBOutlet,而是在您的switch语句中引用sender
  4. switch sender.selectedSegmentIndex
                    {
                case 0:
                    cycleDirection = sender.selectedSegmentIndex
                case 1:
                    cycleDirection = sender.selectedSegmentIndex
                default:
                    break
                }