快速分段控制

时间:2018-08-05 19:16:56

标签: swift

我迅速进行了分段控制,将布尔值更改为true或false;但是,每次在应用程序中选择“ selectedSegmentedIndex == 1”时,都会出现错误“线程1:信号SIGABERT”

我的代码顺其自然:

@IBOutlet weak var translationType: UISegmentedControl!

var state = true

@IBAction func translation(_ sender: Any)
{
    if translationType.selectedSegmentIndex == 0
    {
     state = ture
    }
    else if translationType.selectedSegmentIndex == 1
    {
     state = false
    }
}

任何信息将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:1)

如果未连接sender,则至少使用translationType参数和静态类型可以避免崩溃-这很可能是这种情况。

@IBAction func translation(_ sender: UISegmentedControl)
{
    if sender.selectedSegmentIndex == 0
    {
     state = true
    }
    else if sender.selectedSegmentIndex == 1
    {
     state = false
    }
}

或更短

@IBAction func translation(_ sender: UISegmentedControl)
{
     state = sender.selectedSegmentIndex == 0
}

答案 1 :(得分:0)

var state = true

@IBOutlet weak var translationType: UISegmentedControl!

@IBAction func translation(_ sender: UISegmentedControl)
{
    if translationType.selectedSegmentIndex == 0
    {
        state = true
    }
    else if translationType.selectedSegmentIndex == 1
    {
        state = false
    }

    print(state)
    print(translationType.selectedSegmentIndex)
}

答案 2 :(得分:0)

确保您的插座已连接!请参阅连接检查器