1

Getting rustily back into iOS programming after some years away and am having trouble setting up a UISwitch. Here's my code:

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

The error I get when I toggle the switch is

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

Any thoughts about what I'm doing wrong?

Joel Derfner
  • 2,207
  • 6
  • 33
  • 45
  • Did you connect `firstConjugationVerbSwitch` in storyboard or xib to your class? Seems like you didnt connect it. – adev Dec 29 '17 at 21:33
  • If the IBOutlet wasn't setup correctly then the error would be an unwrapped nil value. This does appear to indicate that it can't find the changeText function. Are they definitely both in the same view controller? Also why not setup an IBAction (not a problem just a query) – Upholder Of Truth Dec 29 '17 at 21:45
  • Well, I looked more closely at the error messages and saw that there was ALSO an 'Unknown Class X in Interface Builder Files' error. I did as https://stackoverflow.com/questions/24924966/xcode-6-strange-bug-unknown-class-in-interface-builder-file?answertab=votes#tab-top suggested, and, though it didn't fix the Unknown Class error, it DID, strangely enough, fix the unrecognized selector error. I'm calling it a day and will deal with the rest over the weekend. But I'm not sure what to do with this question—should I turn this comment into an answer? – Joel Derfner Dec 29 '17 at 22:09
  • Also, @UpholderOfTruth, I'm just running through some exercises first before getting down to what will be a messy business, I'm sure—that's why I didn't just set up the \IBAction. – Joel Derfner Dec 29 '17 at 22:09
  • No worries it's all good while you are learning. If you can turn it into an answer so other people will see it has been resolved. – Upholder Of Truth Dec 29 '17 at 22:18
  • @JoelDerfner the quicker you ditch Interface Builder, the quicker you will pickup `iOS` again. You said you've been away from it for a few years? Well, IB is still junk. – trndjc Dec 30 '17 at 02:28

1 Answers1

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 ..
        }
    }
user3040319
  • 57
  • 1
  • 9