swift buttonクリックイベント

2670 ワード

//
//  ViewController.swift
//  
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // ❗️SWIFT  :
        // Replace 'Selector("buttonTap")' with '#selector(ViewController.buttonTap)'

        // button   
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
        button.backgroundColor = UIColor.yellow
        button.addTarget(self, action: #selector(ViewController.buttonTap), for: UIControlEvents.touchUpInside)
        //button1: 
        let button1 = UIButton(frame: CGRect(x: 100, y: 0, width: 50, height: 30))
        button1.backgroundColor = UIColor.yellow
        button1.addTarget(self, action: #selector(buttonTap1(button:)), for: UIControlEvents.touchUpInside)

        self.view.addSubview(button)
        self.view.addSubview(button1)
    }

    //selector   Objective-C runtime  
    @objc func buttonTap() {
        print("buttonTap")
    }

    @objc func buttonTap1(button:UIButton) {
        print("buttonTap ")
    }




}