【问题标题】:Swift UISegmentedControl IBAction values do not changeSwift UISegmentedControl IBAction 值不会改变
【发布时间】:2014-10-01 02:02:22
【问题描述】:

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

代码如下:

@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("Im in the action") 不会记录。我也尝试过这个好像语句,例如cycleSwitch.selectedSegmentIndex == 0 等等。都不起作用。

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

【问题讨论】:

    标签: swift uisegmentedcontrol


    【解决方案1】:

    需要检查的几件事:

    1. 确保您的分段控件通过其“值已更改”操作连接到@IBAction。 为此,请控制并单击情节提要中的分段控件并仔细检查以确保“值已更改”已连接到您的视图控制器
    2. sender: 参数更改为UISegmentedControl 而不是AnyObject
    3. 不要在 switch 语句中引用 cycleSwitch @IBOutlet,而是引用 sender
    switch sender.selectedSegmentIndex
                    {
                case 0:
                    cycleDirection = sender.selectedSegmentIndex
                case 1:
                    cycleDirection = sender.selectedSegmentIndex
                default:
                    break
                }
    

    【讨论】:

    • 我也试过了。进行您建议的更改并不能解决问题
    • 只是为了验证 - 您已确认 @IBAction 已连接到您的分段控件的“值已更改”操作而不是其他内容?
    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2016-11-14
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多