class ViewController: UIViewController {
var btn:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
btn = UIButton()
createButton("HTTP", i:1, mySelector: "HTTPs")
createButton("SMS", i:2, mySelector: "SMSs")
createButton("Mail", i:3, mySelector: "Mails")
createButton("Tel", i: 4, mySelector: "tellPhome")
}
func createButton(title:String,i:Int=0,mySelector:String){
btn.frame = CGRect(x: 10,y: i*40,width: 200,height: 20)
btn.setTitle(title, forState:UIControlState.Normal)
btn.layer.borderColor = UIColor.blackColor().CGColor
btn.layer.borderWidth = 1
btn.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
btn.addTarget(self, action: Selector("\(mySelector)"), forControlEvents: UIControlEvents.TouchUpInside)
println("\(mySelector)")
self.view .addSubview(btn)
}
func openHTTPs(){
UIApplication.sharedApplication().openURL(NSURL(string: "http:www.baidu.com")!)
}
}
如何让4个button都显示在模拟器上。
上述代码只显示最后一个button。刚学swift,请多坛友们多多指教。 |
|