黑马程序员技术交流社区

标题: swift创建button的问题 [打印本页]

作者: 余永健    时间: 2015-2-9 17:10
标题: swift创建button的问题
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,请多坛友们多多指教。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2