Swift开发:UIButton点击事件

前端之家收集整理的这篇文章主要介绍了Swift开发:UIButton点击事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

值得注意的是 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,forControlEvents: UIControlEvents.TouchUpInside);
        
        btnEditDev.addTarget(self,forControlEvents: UIControlEvents.TouchUpInside);
        
        btnDeleteDev.addTarget(self,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()
        
    }


}
原文链接:https://www.f2er.com/swift/321957.html

猜你在找的Swift相关文章