繁体   English   中英

Swift UISegmentedControl如果更改

[英]Swift UISegmentedControl if change

当UISegmentedControl被检查为另一种情况时,如何始终创建检查?

我创建UISegmentedControl:

var type = 0

 @objc func change(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
      case 0:
        type = 0
      case 1:
        type = 1
      case 2:
        type = 2
      case 3:
        type = 3
      default:
        type = 2
    }

当我需要检查和更改数据时,我会使用func,而SegmentedControl中的更改按钮

func Example() {

  if type == 0 {
    print("case 0")
  } else if type == 1 {
    print("case 1")
  } else if type == 2 {
    print("case 2")
  } else if type == 3 {
    print("case 3")
  }
}

不要通过调用函数进行检查。 将类型更改为动作后,移动要执行的代码

@objc func change(sender: UISegmentedControl) {
     switch sender.selectedSegmentIndex {
     case 0:
         type = 0
         print("case 0")
     case 1:
         type = 1
         print("case 1")
     case 3:
         type = 3
         print("case 3")
     default:
         type = 2
         print("case 2")
    }
}

 
 
 
  
  func Example() { if type == 0 { print("case 0") } else if type == 1 { print("case 1") } else if type == 2 { print("case 2") } else if type == 3 { print("case 3") } }
 
  

我建议将您的问题简化为:

@objc func change(sender: UISegmentedControl) {
    print("case \(sender.selectedSegmentIndex)")
    // do whatever you need with sender.selectedSegmentIndex here directly   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM