无法在Swift 4中设置UISwitch

问题描述 投票:1回答:1

几年之后,在设置UISwitch时遇到麻烦,很快就回到了iOS编程中。这是我的代码:

@IBOutlet weak var firstConjugationVerbSwitch: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(scrollView)
    firstConjugationVerbSwitch.addTarget(self, action: #selector(changeText), for: .valueChanged)
}
@objc func changeText() {
    print("changeText function running.")
}

切换开关时出现的错误是

 [Latin_Substitution_Drills.VerbOptionsViewController firstConjugationVerbSwitch:]: unrecognized selector sent to instance 0x7ffcea817600

关于我做错了什么的任何想法?

ios swift uiswitch
1个回答
0
投票
 let customSwitch = UISwitch(frame:CGRect(x: 20, y: 20, width: 0, height: 0))
        customSwitch.isOn = false
        customSwitch.onTintColor = UIColor(red: 10/255, green: 105/255, blue: 122/255, alpha: 1)
        //customSwitch.setOn(true, animated: true)
        customSwitch.transform = CGAffineTransform(scaleX: 0.60, y: 0.60)
        customSwitch.addTarget(self, action: #selector(switchTarget(sender:)), for: .valueChanged)
        let switchButton = UIBarButtonItem(customView: customSwitch)
 @objc func switchTarget(sender: UISwitch!)
    {
        if sender.isOn {
            // do something ..
        } else{
            // do something ..
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.