Error Message: Cannot find 'UIControlEvent' in scope

I a trying to complete a simple Swift project using code only (instead of the Storyboard, but I keep getting an error message that states: “Cannot find ‘UIControlEvent’ in scope”

I’m getting this error message on the same line as my buttonInstance.addTarget code and I can’t figure out why. Could someone please help me figure out what I’m doing wrong here? Thank you. :frowning:

//
//  ViewController.swift
//  UdacityClickCounterProject
//
//  Created by I on 1/8/21.
//

import UIKit

class ViewController: UIViewController {
    
    var count: Int = 0
    var label: UILabel!


    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
      
        
// ************* LABEL *************
        
      
        let labelInstance = UILabel()
     
        labelInstance.frame = CGRect(x: 150, y: 150, width: 60, height: 60)
 
        labelInstance.text = "0"

        view.addSubview(labelInstance)
        
        self.label = labelInstance

        
// ************* BUTTON *************

        
        let buttonInstance = UIButton()
        
        buttonInstance.frame = CGRect(x: 150, y: 250, width: 60, height: 60)
        
        buttonInstance.setTitle("Click Me!", for: .normal)
        
        buttonInstance.setTitleColor(UIColor.systemPink, for: .normal)
        
        view.addSubview(buttonInstance)
        
        

        buttonInstance.addTarget(self, action: #selector(ViewController.incrementCount), for: UIControlEvent.touchUpInside)

    }
    
   @objc func incrementCount() {
        self.count += 1
        self.label.text = "\(self.count)"
    }


}

You want UIControl.Event.touchUpInside

Or, since the compiler already knows you need a UIControl.Event there, you can just say .touchUpInside. That’s what’s known as an implicit member expression

1 Like

Thank you, Rooster. I’m wondering though, the code that I listed above was taken from my instructor’s code and it worked for her. Did it not work for me because the code is outdated? (The instructor’s code is probably old.)

Probably. UIControlEvent was renamed UIControl.Event with back in 2018 when a lot of existing types were converted to nested types.

1 Like

Thank you so much, Rooster! I appreciate it. :slight_smile: