2016-09-12 8 views
2

inputAccessoryViewでタップすると、UIToolbarに追加するボタンがたくさんあります。これらのボタンのすべては、私が上記のものを達成するための最も効率的な方法だろう何等のSwiftのUIButtonから属性を再利用する最良の方法は

タイトルを除いて同一であると私は何をできるようにしたいと、UIは、titleColorなどの属性に再利用され、frame?ここで

// code for first button 
    let button1 = UIButton(); 
    button1.backgroundColor = UIColor.orangeColor() 
    button1.setTitle("button1", forState: .Normal) 
    button1.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button1.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button1.frame = CGRect(x:0, y:0, width:35, height:35) 
    button1.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 

    // code for second button which is identical to the first button 
    let button2 = UIButton(); 
    button2.backgroundColor = UIColor.orangeColor() 
    button2.setTitle("button2", forState: .Normal) 
    button2.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button2.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button2.frame = CGRect(x:0, y:0, width:35, height:35) 
    button2.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 

    let barButton = UIBarButtonItem() 
    barButton.customView = button 

    let barButton2 = UIBarButtonItem() 
    barButton2.customView = button2 

    let toolBar = UIToolbar() 
    toolBar.items = [ barButton, barButton2] 
    toolBar.sizeToFit() 

    myTextField.inputAccessoryView = toolBar 

答えて

5

は、単一のローカル関数に重複したコードをリファクタリング...コードです。

func configure(_ button:UIButton) { 
    button.backgroundColor = UIColor.orangeColor() 
    button.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
    button.frame = CGRect(x:0, y:0, width:35, height:35) 
    button.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside) 
} 

// code for first button 
let button1 = UIButton() 
button1.setTitle("button1", forState: .Normal) 
configure(button1) 

// code for second button which is identical to the first button 
let button2 = UIButton() 
button2.setTitle("button2", forState: .Normal) 
configure(button2) 
+0

感謝を助けて ! –

3

マットの答え@のようなものが、私見よりよい:

他の場所で、あなたのコード内の
// code for first button 
let button1 = UIButton().configure("button1", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction)) 

// code for second button which is probably not exactly identical to the first button. They should at minimum have different titles, different frames and activate different functions. 
let button2 = UIButton().configure("button2", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction)) 

あなたはおそらく、いくつかのビューコントローラでそれを使用するので:多くのあなたのための

extension UIButton { 
    func configure(title: String, frame: CGRect, target: AnyObject, action: Selector) -> UIButton { 
     self.backgroundColor = UIColor.orangeColor() 
     self.setTitle(title, forState: .Normal) 
     self.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
     self.setTitleColor(UIColor.orangeColor(), forState: .Highlighted) 
     self.frame = frame 
     self.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside) 
     return self 
    } 
} 
+0

私はこのアプローチも好きです、ありがとうございました! –

+1

@fs_tigre彼のアプローチも好きですが、重要な調整が必要です。あなたのデフォルトの大文字小文字のためのenumを加えてください。 [this](http://www.jessesquires.com/enums-as-configs/?utm_campaign=This%2BWeek%2Bin%2BSwift&utm_medium=email&utm_source=This_Week_in_Swift_98)の素晴らしいリンクをご覧ください。 – Honey

関連する問題