Swift UIButton

在Swift中UIButton添加点击事件的最大区别在selector.

下面直接看代码:

//
//  ViewController.swift
//  1.UILabel
//
//  Created by zhuming on 16/1/24.
//  Copyright © 2016年 zhuming. All rights reserved.
//

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    var coun = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label1.text = "第一个Label标签"

        creatLabel2()
        creatButton()
        // Do any additional setup after loading the view, typically from a nib.
    }
    /**
     创建一个标签对象
     */
    func creatLabel2(){
        // 初始化一个label2对象 并设置frame值
        let label2 = UILabel(frame: CGRect(x: 20, y: 200, width: 200, height: 80))
        label2.text = "第二个Label标签2e3gr35hjym eRGBHRTN HRGBHTN H" // 标签显示内容
        label2.textAlignment = NSTextAlignment.Center //对齐方式
        label2.textColor = UIColor.greenColor()   // 字体颜色
        label2.font = UIFont.systemFontOfSize(12) // 字体大小
        label2.backgroundColor = UIColor.grayColor() // 背景颜色
        label2.shadowColor = UIColor.redColor()  // 阴影颜色
        label2.shadowOffset = CGSizeMake(10, 10) // 阴影偏移量
        label2.numberOfLines = 0  // 0:多行显示
        label2.adjustsFontSizeToFitWidth = true  //根据标签长度自动调整字体大小 需要注释掉多行显示才有效果
        // 下面这两个属性 打开字体高亮 和设置字体高亮颜色 会覆盖原来设置的字体颜色
        label2.highlightedTextColor = UIColor.whiteColor()
        label2.highlighted = true
        
        self.view .addSubview(label2)
    }
    func creatButton(){
        let button = UIButton(frame: CGRect(x: 50, y: 300, width: 100, height: 40))
        button.setTitle("按钮", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.redColor()
        button .addTarget(self, action: Selector("btnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        /*
        // OC 里面UIButton添加点击事件
        [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        self.view .addSubview(button)
        */
    }
    func btnClick(){ // 按钮点击事件
        print("按钮按下")
        coun  = coun + 1;
        label1.text = "\(coun)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值