簡體   English   中英

在Swift中將多個UIButton添加到多個UIView

[英]Adding multiple UIButton to multiple UIView in Swift

我在下面編寫了以編程方式創建放置在不同UIView上的多個UIButton代碼。 所有按鈕在按鈕標題上都相似且不同。 該代碼確實完成了所需要做的工作,但是如您所見,該代碼相當冗長,冗長。


如何構造下面的代碼並使它緊湊簡潔?


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)

(快速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)
}

雖然已經回答了這個問題,但我想提出另一種方法,以適應陳雲的回答

同樣,為按鈕創建一個通用方法:

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! String as! Stringas! UIView as! UIView等,簡化並簡化了代碼,使其更加安全。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM