首页 > 代码库 > (一)UI 篇之 Button
(一)UI 篇之 Button
let btn1 = UIButton.buttonWithType(.System) as UIButtonbtn1.backgroundColor = UIColor.redColor()btn1.setTitle("Button1", forState: UIControlState.Normal)btn1.frame = CGRect(x: 10, y: 80, width: 300, height: 30)btn1.tag = 100self.view.addSubview(btn1) let btn2 = UIButton.buttonWithType(.System) as UIButtonbtn2.backgroundColor = UIColor.blueColor()btn2.setTitle("Button2", forState: UIControlState.Normal)btn2.setTitleColor(UIColor.redColor(), forState: .Normal)// set round cornerbtn2.layer.cornerRadius = 10.0btn2.frame = CGRect(x: 10, y: 120, width: 300, height: 30)btn2.tag = 101self.view.addSubview(btn2) let btn3 = UIButton.buttonWithType(.System) as UIButtonbtn3.setTitle("Button3", forState: UIControlState.Normal)btn3.layer.cornerRadius = 10.0// set borderbtn3.layer.borderWidth = 1btn3.layer.borderColor = UIColor.blueColor().CGColorbtn3.frame = CGRect(x: 10, y: 160, width: 300, height: 30)btn3.tag = 102self.view.addSubview(btn3) // set listeners// Selector is a struct it convert a string to C pointerbtn1.addTarget(self, action: Selector("onBtnClick:"), forControlEvents: UIControlEvents.TouchUpInside)btn2.addTarget(self, action: Selector("onBtnClick:"), forControlEvents: UIControlEvents.TouchUpInside)btn3.addTarget(self, action: Selector("onBtnClick:"), forControlEvents: UIControlEvents.TouchUpInside)
点击事件
func onBtnClick(btn:UIButton){ var msg = "" switch btn.tag{ case 100: msg = "you clicked button1" case 101: msg = "you clicked button2" case 102: msg = "you clicked button3" default: println("default") } // In IOS8 the UIAlertView is deprecated // now we use UIAlertController var alert = UIAlertController(title: "Alert", message: msg, preferredStyle: UIAlertControllerStyle.Alert) var ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:{ (action) in println("you clicked ok") }) alert.addAction(ok) self.presentViewController(alert, animated: true)}
代码地址 https://github.com/lesliebeijing/IOSWithSwiftShowcase.git
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。