在Swift中UIbutton添加点击事件的最大区别在selector.
下面直接看代码:
//// VIEwController.swift// 1.UILabel//// Created by zhuming on 16/1/24.// copyright © 2016年 zhuming. All rights reserved.//import UIKitclass 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. }}效果图:
总结
以上是内存溢出为你收集整理的Swift UIButton全部内容,希望文章能够帮你解决Swift UIButton所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)