如何在viewDidLoad(Swift)上创建多个UIButton

时间:2014-09-28 09:10:11

标签: swift uibutton

对不起,但如果我有一些来自plist文件的按钮...... 如何重新创建所有按钮? 示例我尝试了这种方式:

 func loadSavedButtons() {
 // I extract the number from the plist file 
 var number: AnyObject? = data.valueForKey("NumberOfButton")
 var someNumb: Int = ("\(number!)").toInt()!
 if (someNumb != 0) {
 self.countButton = someNumb
 for (var i=0; i < someNumb; i++) {

在这一点上,我应该做

 var newButton6: () = CreateButtonWithIndex(i)
 self.view.addSubview(newButton6)
 }}}

 @IBAction func CreateButtonWithIndex(sender: AnyObject){
 let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
 ecc…
 self.view.addSubview(button)

但是不要......我没有全部保存按钮请帮助我!!!

1 个答案:

答案 0 :(得分:0)

在你的功能中 更改以下

let button = UIButton.buttonWithType(UIButtonType.System) as UIButton

var button = UIButton.buttonWithType(UIButtonType.System) as UIButton

之后

self.view.addSubview(button) 

添加以下内容:

button = UIButton.new()