2017-10-21 185 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