2017-10-21 13 views
0

以下のコードは、UIViewという異なる場所に複数のUIButtonをプログラムで作成したものです。すべてのボタンはボタンのタイトルによって似ていて異なっています。コードは実行する必要があるコードを実行しますが、わかるように、コードはかなり冗長で、長すぎます。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) 
+0

この質問はおそらく、コアレビューフォーラム(https://codereview.stackexchange.com)に最も適していますが、私の頭の上からは、UIButtonを拡張して、各ボタンのコードをラップする「便利な」初期化子を作成してください単一の呼び出しに? 'addTarget'パラメータを初期化子に渡すことさえできます。 (私はいつもこれをやっています) – dfd

+0

サブクラスのuibutton –

答えて

1

(スウィフト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

関連する問題