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

时间:2016-09-12 01:32:40

标签: ios swift uibutton uitextfield uitoolbar

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

实现上述目标的最有效方法是什么?

这是代码......

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:3)

像@ 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
    }
}