【问题标题】:What is the best way to reuse the attributes from a UIButton in Swift在 Swift 中重用 UIButton 属性的最佳方法是什么
【发布时间】:2017-01-19 10:02:54
【问题描述】:

当使用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

【问题讨论】:

    标签: ios swift uibutton uitextfield uitoolbar


    【解决方案1】:

    类似于@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
        }
    }
    

    【讨论】:

    • 我也喜欢这种方法,非常感谢您的帮助!
    • @fs_tigre 我也喜欢他的方法,但有一个重要的调整。为您的默认情况添加一个枚举。见this很棒的链接。
    【解决方案2】:

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

    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)
    

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2011-11-11
    • 2016-02-16
    相关资源
    最近更新 更多