Swift 快速分段控制

Swift 快速分段控制,swift,Swift,我在swift中创建了一个分段控件,将布尔值更改为true或false;但是,每次我在应用程序中选择“selectedSegmentedIndex==1”,我都会得到错误“Thread1:signal SIGABERT” 我的代码如下所示: @IBOutlet weak var translationType: UISegmentedControl! var state = true @IBAction func translation(_ sender: Any) { if tra

我在swift中创建了一个分段控件,将布尔值更改为true或false;但是,每次我在应用程序中选择“selectedSegmentedIndex==1”,我都会得到错误“Thread1:signal 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
    }
}

如有任何信息,将不胜感激。谢谢。

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

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

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

我猜您没有正确链接此行
@IBOutlet弱var translationType:UISegmentedControl与故事板中的对应项。是的,听起来不错,因为代码工作完美如果
translationType
nil
也会崩溃如果translationType为nil而不是uts未正确连接myfriend,请从故事板重新连接它是的,因此,你的代码没有任何区别。只是想帮助我亲爱的朋友这可能是一个评论。