2017-10-21 125 views
0

我写了下面的代码编程创建多个UIButton被放置在不同的UIView。按钮标题的所有按钮都相似且不同。代码没有完成它所需要做的,但你可以看到,代码是相当冗长,它太冗长。添加多个UIButton的多个UIView的斯威夫特


问题

我怎样组织下面的代码,使之紧凑,简洁的?


代码

let myButton0 = UIButton(type: UIButtonType.Custom) 
myButton0.setTitle("Text 0", forState:.Normal) 
myButton0.titleLabel!.font = UIFont.systemFontOfSize(12) 
myButton0.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
myButton0.backgroundColor = UIColor.darkGrayColor() 
myButton0.frame = CGRectMake(0, 0, 200, 100) 
myButton0.alpha = 1 
myButton0.showsTouchWhenHighlighted = false 
myButton0.adjustsImageWhenHighlighted = false 
myButton0.addTarget(self, action: #selector(ViewController.goDoThis0), forControlEvents:.TouchUpInside) 
myView0.addSubview(myButton0) 

let myButton1 = UIButton(type: UIButtonType.Custom) 
myButton1.setTitle("Text 1", forState:.Normal) 
myButton1.titleLabel!.font = UIFont.systemFontOfSize(12) 
myButton1.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
myButton1.backgroundColor = UIColor.darkGrayColor() 
myButton1.frame = CGRectMake(0, 0, 200, 100) 
myButton1.alpha = 1 
myButton1.showsTouchWhenHighlighted = false 
myButton1.adjustsImageWhenHighlighted = false 
myButton1.addTarget(self, action: #selector(ViewController.goDoThis1), forControlEvents:.TouchUpInside) 
myView1.addSubview(myButton1) 

let myButton2 = UIButton(type: UIButtonType.Custom) 
myButton2.setTitle("Text 2", forState:.Normal) 
myButton2.titleLabel!.font = UIFont.systemFontOfSize(12) 
myButton2.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
myButton2.backgroundColor = UIColor.darkGrayColor() 
myButton2.frame = CGRectMake(0, 0, 200, 100) 
myButton2.alpha = 1 
myButton2.showsTouchWhenHighlighted = false 
myButton2.adjustsImageWhenHighlighted = false 
myButton2.addTarget(self, action: #selector(ViewController.goDoThis2), forControlEvents:.TouchUpInside) 
myView2.addSubview(myButton2) 

let myButton3 = UIButton(type: UIButtonType.Custom) 
myButton3.setTitle("Text 3", forState:.Normal) 
myButton3.titleLabel!.font = UIFont.systemFontOfSize(12) 
myButton3.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
myButton3.backgroundColor = UIColor.darkGrayColor() 
myButton3.frame = CGRectMake(0, 0, 200, 100) 
myButton3.alpha = 1 
myButton3.showsTouchWhenHighlighted = false 
myButton3.adjustsImageWhenHighlighted = false 
myButton3.addTarget(self, action: #selector(ViewController.goDoThis3), forControlEvents:.TouchUpInside) 
myView3.addSubview(myButton3) 

let myButton4 = UIButton(type: UIButtonType.Custom) 
myButton4.setTitle("Text 4", forState:.Normal) 
myButton4.titleLabel!.font = UIFont.systemFontOfSize(12) 
myButton4.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
myButton4.backgroundColor = UIColor.darkGrayColor() 
myButton4.frame = CGRectMake(0, 0, 200, 100) 
myButton4.alpha = 1 
myButton4.showsTouchWhenHighlighted = false 
myButton4.adjustsImageWhenHighlighted = false 
myButton4.addTarget(self, action: #selector(ViewController.goDoThis4), forControlEvents:.TouchUpInside) 
myView4.addSubview(myButton4) 
+0

这个问题可能最适合在核心评论论坛 - https://codereview.stackexchange.com - 但我的头顶,为什么不扩展'UIButton',创建一个'方便'初始值设定项,它包装每个按钮的代码成一个单一的电话?您甚至可以将'addTarget'参数传递给初始值设定项。 (我做这一切的时候。) – dfd

+0

子类UIButton的 –

回答

1

(SWIFT 4.0)
首先,写用于创建按钮的常用方法:

func createButton(title:String,toView:UIView,action:Selector) { 
    let myButton = UIButton(type: UIButtonType.custom) 
    myButton.setTitle(title, for:.normal) 
    myButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) 
    myButton.setTitleColor(UIColor.white, for: .normal) 
    myButton.backgroundColor = UIColor.darkGray 
    myButton.frame = CGRect(x: 0, y: 0, width: 200, height: 100) 
    myButton.alpha = 1 
    myButton.showsTouchWhenHighlighted = false 
    myButton.adjustsImageWhenHighlighted = false 
    myButton.addTarget(self, action: action, for: .touchUpInside) 
    toView.addSubview(myButton) 
} 

然后创建这样的按钮:

let buttonInfos = [ 
         ["Text 0",myView0,#selector(ViewController.goDoThis0)], 
         ["Text 1",myView1,#selector(ViewController.goDoThis1)], 
         ["Text 2",myView2,#selector(ViewController.goDoThis2)], 
         ["Text 3",myView3,#selector(ViewController.goDoThis3)], 
         ["Text 4",myView4,#selector(ViewController.goDoThis4)], 
        ] 

for buttonInfo in buttonInfos { 
    self.createButton(title: buttonInfo[0] as! String, toView: buttonInfo[1] as! UIView, action: buttonInfo[2] as! Selector) 
} 
+0

完美。解释和代码向我展示了一种新技术。非常感谢! – user4806509

+1

:),欢迎您! –

1

虽然这个问题已经回答了,但我想提供另一种方法,改编自Yun CHEN's answer

同样,对于您的按钮创建一个通用的方法:

func createButton(title:String,toView:UIView,action:Selector) { 
    let myButton = UIButton(type: UIButtonType.custom) 
    myButton.setTitle(title, for:.normal) 
    myButton.titleLabel?.font = UIFont.systemFont(ofSize: 12) 
    myButton.setTitleColor(UIColor.white, for: .normal) 
    myButton.backgroundColor = UIColor.darkGray 
    myButton.frame = CGRect(x: 0, y: 0, width: 200, height: 100) 
    myButton.alpha = 1 
    myButton.showsTouchWhenHighlighted = false 
    myButton.adjustsImageWhenHighlighted = false 
    myButton.addTarget(self, action: action, for: .touchUpInside) 
    toView.addSubview(myButton) 
} 

然后在元组数组列出按钮信息:

let buttonInfos = [ 
         ("Text 0",myView0,#selector(ViewController.goDoThis0)), 
         ("Text 1",myView1,#selector(ViewController.goDoThis1)), 
         ("Text 2",myView2,#selector(ViewController.goDoThis2)), 
         ("Text 3",myView3,#selector(ViewController.goDoThis3)), 
         ("Text 4",myView4,#selector(ViewController.goDoThis4)), 
        ] 

最后创建像这样的按钮:

for buttonInfo in buttonInfos { 
    self.createButton(title: buttonInfo.0, toView: buttonInfo.1, action: buttonInfo.2) 
} 

正如你在你的案例中看到的那样,通过使用元组,你不需要将类型转换为as! Stringas! UIView等,简化并使得代码更短,更安全。

+0

感谢这个替代品! – user4806509

+0

不客气! :) – LWJ