0

I have an extension class, where I'm extending UIButton like below, it's working fine.

extension UIButton {    
    class func backButtonTarget(_ target: Any, action: Selector) -> UIBarButtonItem {        
        let backButton = UIButton(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(58), height: CGFloat(15)))
        backButton.setTitle("Cancel",for: .normal)
        let barBackButtonItem = UIBarButtonItem(customView: backButton)
        backButton.addTarget(target, action: action, for: .touchUpInside)
        return barBackButtonItem
    }
}

But now, I need to change its title for some view controller, so I was thinking of its overriding, but failed. How can be this be overridden, so that I can change its title?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46

2 Answers2

2

Extensions can not/should not override.

It is not possible to override functionality (like properties or methods) in extensions as documented in Apple's Swift Guide.

Extensions can add new functionality to a type, but they cannot override existing functionality.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

You can subclass the bar button Item like

class CustomBarButtonItem: UIBarButtonItem {

      override func awakeFromNib() {
      super.awakeFromNib()
      customize()
     }

     func customize() {
         frame = CGRect(x: CGFloat(0), y:    CGFloat(0), width: CGFloat(58), height: CGFloat(15))
         // Add more as per requirement
     }

}
rajtharan-g
  • 432
  • 5
  • 14
  • I appreciate your answer, Thanks, But i have to change its tittle. – Abhishek Mitra Oct 13 '17 at 11:22
  • You can set the title by overriding the CustomBarButtonItem in the view controller where the title needs to be changed. You can set default title in the above class. In case where change needs, you can override, call super method and change the title as needed. – rajtharan-g Oct 13 '17 at 11:23
1

Make title a String parameter for the function, so you can call it and provide a custom title.

class func backButtonTarget(_ target: Any, action: Selector, title: String) -> UIBarButtonItem {
        let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 58, height: 15))
        backButton.setTitle(title, for: .normal)
        let barBackButtonItem = UIBarButtonItem(customView: backButton)
        backButton.addTarget(target, action: action, for: .touchUpInside)
        return barBackButtonItem
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • yes, I had thought about it, but i have to change it in uncountable classes. which i don't want. – Abhishek Mitra Oct 13 '17 at 11:16
  • you can make the title optional, and if it is e.g. `nil`, you'd keep the original title, so you don't need to change anything in the existing code but the places where you need to present a specific title. – holex Oct 13 '17 at 11:17
  • I appreciate your answer, could you please tell me how to make it optional in this code ? – Abhishek Mitra Oct 13 '17 at 11:21
  • 1
    `class func backButtonTarget(_ target: Any, action: Selector, title: String? = nil) -> UIBarButtonItem { ... if let title = title { backButton.setTitle(title, for: .normal) } ... }`, or something like that could do the job _(I don't intent to copy @the4kman's answer to make another one, but you are supposed to get the gist by my comment)_. – holex Oct 13 '17 at 11:24
  • Thanks, Thats enough, let me try this and come back to you. – Abhishek Mitra Oct 13 '17 at 11:25