Swift按钮 (UIButton)的用法

import UIKit

class ViewController: UIViewController        ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet  Delegate,UIAlertViewDelegate{
override func viewDidLoad() {
    super.viewDidLoad()
    
    let button:UIButton = UIButton(type: UIButtonType.Custom)
    
    button.frame = CGRectMake(10, 80, 200, 30)
    
//  button.backgroundColor = UIColor.purpleColor()
    
    button.setTitle("按钮", forState: UIControlState.Normal)
    
    button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
    button.setTitleColor(UIColor.greenColor(), forState: UIControlState.Highlighted)
    button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)////禁用状态下文字的颜色
    button.setTitleShadowColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    button.setTitleShadowColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
    button.setTitleShadowColor(UIColor.orangeColor(), forState: UIControlState.Disabled)//禁用状态下阴影文字的颜色
    
   
    //按钮文字图标的设置
    button.setBackgroundImage(UIImage(named: "lalala"), forState:.Normal)
    //1不传递触摸对象(即点击的按钮)
    button.addTarget(self, action: Selector("tapped"), forControlEvents: UIControlEvents.TouchUpInside)
    //2.传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
    button.addTarget(self, action: Selector("tapped:"), forControlEvents: UIControlEvents.TouchUpInside)
    
    self.view.addSubview(button) 
 
}

//1不传递触摸对象(即点击的按钮)
func tapped(){
    print("tapped")
}

//2.传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
func tapped(button:UIButton){
    print("tapped haha")
}



}

你可能感兴趣的:(Swift按钮 (UIButton)的用法)