在 Swift 中重用 UIButton 的属性的最佳方法是什么

What is the best way to reuse the attributes from a UIButton in Swift

当使用 inputAccessoryView 点击 UITextField 时,我有一堆按钮添加到 UIToolbar。除了标题之外,所有这些按钮都是相同的,我希望能够重复使用 UI 属性,例如 titleColorframe

完成上述内容的最有效方法是什么?

这是代码...

    // 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

将重复代码重构为单个本地函数。

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)

类似于@matt 的回答,但恕我直言更好:

// 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
    }
}