Swift-UIButton&AlertView

大家看到题目应该就知道我要说的东西了,今天要给大家介绍button控件,也是比较常用的用法。我就不啰嗦什么了。直接上代码

var myButton: UIButton!

//Attention: buttonWithType return the type of AnyObject

myButton = UIButton.buttonWithType(.System) as? UIButton(这里有必要说明一下,buttonWithType最终返回的是一个AnyObject,所以这里要用到类型转换)

//Set the location and size of button

myButton.frame = CGRectMake(110,70,200, 100)

//Add a name to button,and set state

myButton.setTitle("Press Me",forState: .Normal)

myButton.setTitle("I`m Pressed",forState: .Highlighted)

//Add action to button

myButton.addTarget(self,action: "buttonIspressed:",forControlEvents: .TouchDown)

myButton.addTarget(self,action: "buttonIsTaped:",forControlEvents: .TouchUpInside)

//Add backgroundColor

myButton.backgroundColor = UIColor.greenColor()

//Add image to backgound of myButton

var image = UIImage(named: "pg.jpg")

myButton.setBackgroundImage(image,forState: .Normal)

view.addSubview(myButton)

到这里button的用法差不多就讲完了。下面我要说的是AlertView,不是AlertViewController。大家看仔细:

myAlertView = UIAlertView(title: "Done",message: "The button is tapped",delegate: self,cancelButtonTitle: "Cancell",otherButtonTitles: "ok")

myAlertView!.show()

Ok,今天就说这么多了。明天一个下载图片的demo,最近做项目要用。

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...