Swift开发:UIButton点击事件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/taoerit/article/details/62418409

  值得注意的是 addTarget 中action 的参数,点击事件字符串后面要加 “:  ”

//
//  ViewController.swift
//  CBControler
//
//  Created by 黄涛 on 17/3/16.
//  Copyright © 2017年 huangtao. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var topLabel: UILabel!
    
    @IBOutlet weak var btnConnectDev: UIButton!
    
    @IBOutlet weak var btnAddDev: UIButton!
    
    @IBOutlet weak var btnEditDev: UIButton!
    
    @IBOutlet weak var btnDeleteDev: UIButton!
    
    func initButton(){
        btnConnectDev.tag = 2000;
        btnAddDev.tag = 2001;
        btnEditDev.tag = 2002;
        btnDeleteDev.tag = 2003;
        
        btnConnectDev.addTarget(self, action: "btnClickFun:", forControlEvents: UIControlEvents.TouchUpInside);
        
        btnAddDev.addTarget(self, action: "btnClickFun:", forControlEvents: UIControlEvents.TouchUpInside);
        
        btnEditDev.addTarget(self, action: "btnClickFun:", forControlEvents: UIControlEvents.TouchUpInside);
        
        btnDeleteDev.addTarget(self, action: "btnClickFun:", forControlEvents: UIControlEvents.TouchUpInside);
    }
    
    func btnClickFun(sender:UIButton?){
        
        let tag = sender?.tag;
        switch(tag!)
        {
            
        case 2000:// 连接设备
            print("连接设备");
            break;
        case 2001:// 添加设备
            print("添加设备");
            break;
        case 2002:// 编辑设备
            print("编辑设备");
        break;
        case 2003://移除设备
            print("移除设备");
            break;
        default:
            break;

            
        }
    
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化按钮
        initButton();
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


}


















展开阅读全文

没有更多推荐了,返回首页