gpt4 book ai didi

ios - Swift 无法识别的选择器发送到实例分段控件

转载 作者:搜寻专家 更新时间:2023-10-31 21:54:18 25 4
gpt4 key购买 nike

我试图将选择器添加到我的 UISegmentedControl。

    segmentedControl = UISegmentedControl(items: items)
segmentedControl.layer.cornerRadius = 12.0
segmentedControl.layer.borderColor = UIColor.purpleLight.cgColor
segmentedControl.layer.borderWidth = 1.0
segmentedControl.layer.masksToBounds = true
segmentedControl.backgroundColor = .white
self.contentView.addSubview(segmentedControl)

segmentedControl.addTarget(self, action: Selector(("changeColor:")), for:.valueChanged)

然后:

func changeColor(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 1:
segmentedControl.backgroundColor = UIColor.green
case 2:
segmentedControl.backgroundColor = UIColor.blue
default:
segmentedControl.backgroundColor = UIColor.purple
}
}

但是,当我点击它时,出现错误 - unrecognized selector sent to instance 0x7fcf5f049000

最佳答案

替换您的操作参数 - 选择器。自 Swift 3 以来,选择器语法已更改。

segmentedControl.addTarget(self, action: #selector(self.changeColor(sender:)), for:.valueChanged)

关于ios - Swift 无法识别的选择器发送到实例分段控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com