如何使用swift在Xcode中创建自定义按钮?

时间:2014-11-18 01:35:03

标签: ios swift uibutton xcode6

我尝试使用swift在Xcode 6中制作自定义按钮,但我不确定如何继续。我应该使用助理编辑器在我的viewController.swift文件中创建一个类,然后将该类应用于有问题的按钮?

1 个答案:

答案 0 :(得分:2)

这是一个你好世界的例子。

class myViewcontroller: UIViewController{
var helloLabel : UILabel!
var abutton   :UIButton!

func viewTapped(sender : AnyObject) {
    if (helloLabel.text == "tap it again please good sir"){
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    helloLabel.text = "tap it again please good sir"

}
override func awakeFromNib() {
    super.awakeFromNib()
        }
override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.redColor()
    helloLabel = UILabel(frame: CGRectMake(50, 50, 150, 50))
    helloLabel.text = "hello world"
    helloLabel.textColor = UIColor.whiteColor()

    abutton = UIButton(frame: CGRectMake(50, 100, 100, 50))
    abutton.backgroundColor = UIColor.greenColor()
    abutton.setTitle("Test Button", forState: UIControlState.Normal)
    abutton.addTarget(self, action: "viewTapped:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(abutton)

    self.view .addSubview(helloLabel)
    // Do any additional setup after loading the view, typically from a nib.

    }
 }