How can I override this class function? - ios

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?

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

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

Related

How to make globe method for custom button action in swift

I have a sign in screen in which two fields are there password and email textfield.
I added both textfield with stackview. After adding textfields, I need to add button of password lock. After clicking on password lock button user can see the password. I took button from library and try to add on textfield but, due to stackview it always set as third element for stackview. So I am unable to add.
So I decided to add with extension of textfield. So i added button successfully in utility class,
but unable to add action with selector. It showing an error
Value of type 'UIViewController' has no member 'passwordAction'
My code for add button is
extension UITextField {
func passwordButton(vc:UIViewController){
let paddingView = UIView(frame: CGRect(x: 25, y: 25, width: 24, height: 17))
let passwordButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 17))
passwordButton.setImage(UIImage(named: "show_password"), for: .normal)
passwordButton.addTarget(vc, action: #selector(vc.passwordAction), for: .touchUpInside)
paddingView.addSubview(passwordButton)
self.addSubview(paddingView)
}
}
class Loginviewcontroller{
passwordTextField.passwordButton(vc: self)
func passwordAction(){
print("action")
}
}
I am calling this method from login controller.
So I have two question:-
when two textfield are attached with stackview, we can not put button on textfield with storyboard?
How can I make globle method to add button and add action that can access in uiviewcontroller?
Error is self explanatory, you added the passwordAction method to Loginviewcontroller but in func passwordButton you take a UIViewController as an argument. As a result even when you pass instance of Loginviewcontroller to passwordButton function call, in the function scope vc is just another UIViewController and clearly all UIViewControllers does not have a method named passwordAction.
As you said you wanna make global method to add button and add action that can access in uiviewcontroller you can follow below approach
Step 1: Declare a protocol
#objc protocol SecureTextProtocol where Self: UIViewController {
func passwordAction()
}
Step 2: Update your passwordButton method to take SecureTextProtocol instead of plain UIViewController
extension UITextField {
func passwordButton(vc:SecureTextProtocol){
let paddingView = UIView(frame: CGRect(x: 25, y: 25, width: 24, height: 17))
let passwordButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 17))
passwordButton.setImage(UIImage(named: "show_password"), for: .normal)
passwordButton.addTarget(vc, action: #selector(vc.passwordAction), for: .touchUpInside)
paddingView.addSubview(passwordButton)
self.addSubview(paddingView)
}
}
Step 3: Make your ViewController's that want to get the button action to confirm to SecureTextProtocol protocol
class Loginviewcontroller: UIViewController, SecureTextProtocol {
passwordTextField.passwordButton(vc: self)
func passwordAction(){
print("action")
}
}
That should do the job
You can always solve this problem with simple code, that's the way I do it.
extension UITextField {
func setPasswordToggleImage(_ button: UIButton) {
if(isSecureTextEntry){
button.setImage(UIImage(named: "ic_password_visible"), for: .normal)
}else{
button.setImage(UIImage(named: "ic_password_invisible"), for: .normal)
}
}
func enablePasswordToggle(){
let button = UIButton(type: .custom)
setPasswordToggleImage(button)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
self.rightView = button
self.rightViewMode = .always
}
#objc func togglePasswordView(_ sender: Any) {
self.isSecureTextEntry = !self.isSecureTextEntry
setPasswordToggleImage(sender as! UIButton)
}
}
then on viewcontroller's viewdidload just call the functions on your storyboards
override func viewDidLoad() {
super.viewDidLoad()
txtPassword.enablePasswordToggle()
txtConfirmPassword.enablePasswordToggle()
}

UIButton Extension For UIBarButtonItem

I'm writing an extension by taking a swift file and did codes in following way:
import Foundation
import UIKit
extension UIButton{
func backButtonTarget(_ target: Any, action: Selector) -> UIBarButtonItem {
let backButton = UIButton(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(12), height: CGFloat(20)))
backButton.setBackgroundImage(UIImage(named: "back.png"), for: .normal)
let barBackButtonItem = UIBarButtonItem(customView: backButton)
backButton.addTarget(target, action: action, for: .touchUpInside)
return barBackButtonItem
}
}
Now, I tried to use it by declaring a button in my viewcontroller where i need that by following way:
var backButton: UIButton! = nil
and in my viewDidLoad I'm declaring the following code:
self.navigationItem.leftBarButtonItem = backButton?.backButtonTarget(self, action: #selector(self.popCurrentViewController))
and in popCurrentViewController
func popCurrentViewController() -> Void {
_ = self.navigationController?.popViewController(animated: true)
}
Here, by declaring above code, UINavigationBar leftbar item button should show as per my created button, but it is not showing neither it enter into the extension function either, but perfectly working on Objective c codes by using category, Where i'm doing wrong/ mistake here ? Any help will be appreciable.
Thanks
There is no button in navigationBar is because your backButton is nil, Currently you have added extension with instance method instead of that you need class method.
extension UIButton {
class func backButtonTarget(_ target: Any, action: Selector) -> UIBarButtonItem {
let backButton = UIButton(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(12), height: CGFloat(20)))
backButton.setBackgroundImage(UIImage(named: "back.png"), for: .normal)
let barBackButtonItem = UIBarButtonItem(customView: backButton)
backButton.addTarget(target, action: action, for: .touchUpInside)
return barBackButtonItem
}
}
Now add BarButtonItem this way.
self.navigationItem.leftBarButtonItem = UIButton.backButtonTarget(self, action: #selector(self.popCurrentViewController))

Eliminate repeated code Swift 3

I have multiple navigation controllers and their root view controllers in my app. I want each navigation bar to have social media buttons closely placed on the right side of the bar. For the same I have used this code to show the buttons in 1 view controller:
let fbImage = UIImage(named: "Facebook.png")!
let twitterImage = UIImage(named: "Twitter.png")!
let youtbImage = UIImage(named:"YouTube.png")!
let fbBtn: UIButton = UIButton(type: .custom)
fbBtn.setImage(fbImage, for: UIControlState.normal)
fbBtn.addTarget(self, action: #selector(HomeViewController.fbBtnPressed), for: UIControlEvents.touchUpInside)
fbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let fbBarBtn = UIBarButtonItem(customView: fbBtn)
let twitterBtn: UIButton = UIButton(type: .custom)
twitterBtn.setImage(twitterImage, for: UIControlState.normal)
twitterBtn.addTarget(self, action: #selector(HomeViewController.twitterBtnPressed), for: UIControlEvents.touchUpInside)
twitterBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let twitterBarBtn = UIBarButtonItem(customView: twitterBtn)
let youtbBtn: UIButton = UIButton(type: .custom)
youtbBtn.setImage(youtbImage, for: UIControlState.normal)
youtbBtn.addTarget(self, action: #selector(HomeViewController.youtubeBtnPressed), for: UIControlEvents.touchUpInside)
youtbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let youtbBarBtn = UIBarButtonItem(customView: youtbBtn)
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
Now I want the same buttons on all navigations bars. I can easily copy this code and respective target methods in viewDidLoad() of each view controller, but too much code is getting repeated. So how can avoid this situation?
I am using Swift 3. I am new to iOS. Any help will be appreciated!
Most duplications are solved by using functions. The first step is to extract that code into a function, the second step is to use the same function from multiple places.
You can add it to an extension, for example:
extension UIViewController {
func addShareButtons() {
...
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
}
}
and only call
self.addShareButtons()
from every controller that needs the buttons.
You can add button handlers to the extension too.
Another method is to use a UIViewController subclass but that's always a problem if you want to use a UITableViewController subclass.
You can design a custom navigation controller and add these code to the navigation controller. inherit the navigation controller to your storyboard or programmatically where you want to use.
//sample code
class "YourNavigationCorollerName": UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//declare here you code, what you want
}
}
Assuming that these buttons ALWAYS have the same behavior, you can create a custom class for them, and place the repeated code there. For instance:
class FacebookButton : UIButton {
override init() {
super.init()
self.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.setImage(UIImage(named: "Facebook.png")!, for: .normal)
}
}
...I think the compiler will yell at you for initializing a UIView without a decoder and frame, but hopefully you get the idea.
To improve on this, you could 1) create a custom UINavigationController with the UIBarButtonItems you want to use, and reuse that controller, and/or 2) create a protocol like the following:
protocol FacebookBtnHandler {
func fbBtnPressed()
}
...and have any relevant VCs conform to it. This would allow you to assign the target and selector for the button in the FacebookButton init method, where you assign the image and frame, and hence prevent repetition of that line, as well.
Try to implement bar button by creating subclass of UIBarButton
class Button: UIBarButtonItem {
convenience init(withImage image : UIImage,Target target: Any, andSelector selector: Any?){
let button = UIButton(type: .custom)
button.setImage(image, for: UIControlState.normal)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
button.addTarget(target, action: selector, for: UIControlEvents.touchUpInside)
self.init(customView: button)
}
}
let fbImage = UIImage(named: "Facebook.png")!
let twitterImage = UIImage(named: "Twitter.png")!
let youtbImage = UIImage(named:"YouTube.png")!
let fbBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.fbBtnPressed))
let twitterBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.twitterBtnPressed))
let youtubeBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.youtubeBtnPressed))
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
and make it for all your view controller
extension UIViewController {
func addButtons() {
// add above code
}
}

Stuck with adding target to button programmatically

I've created a UIButton and I want it to print some message when it's pressed.
So I did something like this:
In loadView()
button.addTarget(self, action: #selector(ViewController.pressButton(button:)), for: .touchUpInside)
A method:
func pressButton(button: UIButton) {
NSLog("pressed!")
}
But nothing happens when I click the button.
Add the button code in your viewDidLoad and it will work for you:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = UIColor.gray
button.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
self.view.addSubview(button)
}
func pressButton(button: UIButton) {
NSLog("pressed!")
}
You don´t need to add ViewController.pressButton to selector, it´s enough with the function name.
Swift 4.x version:
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = .gray
button.tag = 0
button.addTarget(self, action: #selector(pressButton(_:)), for: .touchUpInside)
self.view.addSubview(button)
#objc func pressButton(_ button: UIButton) {
print("Button with tag: \(button.tag) clicked!")
}
Swift 5.1 version:
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button.backgroundColor = .gray
button.tag = 100
button.addTarget(self, action: #selector(pressButton), for: .touchUpInside)
self.view.addSubview(button)
#objc func pressButton(button: UIButton) {
print("Button with tag: \(button.tag) clicked!")
}
try this in Swift3!
button.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
#objc func pressButton(button: UIButton) { NSLog("pressed!") }
Use the following code.
button.addTarget(self, action: #selector(FirstViewController.cartButtonHandler), for: .touchUpInside)
Your class name corresponds to FirstViewController
And your selector corresponds to the following function
func cartButtonHandler() {
}
In swift 3 use this -
object?.addTarget(objectWhichHasMethod, action: #selector(classWhichHasMethod.yourMethod), for: someUIControlEvents)
For example(from my code) -
self.datePicker?.addTarget(self, action:#selector(InfoTableViewCell.datePickerValueChanged), for: .valueChanged)
Just give a : after method name if you want the sender as parameter.
You mention that the addTarget call is in loadView(). Is this in your custom subview, of some kind, or the viewController?
From your selector, it's targeting a method in your ViewController class, but if the target for this action is the view itself, then it would make sense that the action is not going through.
If you declare your button in a viewController, and in viewDidLoad add this target as above, then the message should be printed as you're looking for. I believe you are "targetting" the wrong class with your action.
let cancelButton = UIButton.init(frame: CGRect(x: popUpView.frame.size.width/2, y: popUpView.frame.size.height-20, width: 30, height: 30))
cancelButton.backgroundColor = UIColor.init(patternImage: UIImage(named: cancelImage)!)
cancelButton.addTarget(self, action: #selector(CommentsViewController.canceled), for:.touchUpInside)
Add the button code in override func viewDidLoad() method
Make sure your action handler tagged with #IBAction like this:
#IBAction func pressButton(button: UIButton) {
print("pressed!")
}
Then it will work!

Swift3: Add button with code

Im reading Apples swift (iOS) documentation but its written for Swift 2 and i use Swift 3. I want to add a button programmatically but its seems there is a change and I can't find how to fix it.
Here is the Code for the Swift 2 example:
import UIKit
class RatingControl: UIView {
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Buttons
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red()
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(button: UIButton){
print("Button pressed")
}
}
The only change i made after the 'fix-it' showed the error is this in the selector:
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchDown)
This should have printed "Button pressed" but it doesn't. Any help?
My code:
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), for: .touchDown)
override var intrinsicContentSize : CGSize {
//override func intrinsicContentSize() -> CGSize {
//...
return CGSize(width: 240, height: 44)
}
// MARK: Button Action
func ratingButtonTapped(_ button: UIButton) {
print("Button pressed 👍")
}
Try something like this. I haven't tested but it should work:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(ratingButtonTapped), for: .touchUpInside)
addSubview(button)
func ratingButtonTapped() {
print("Button pressed")
}
Found the solution. For some reason the:
func ratingButtonTapped(button: UIButton)
needs an "_" before button. So it should be :
func ratingButtonTapped(_ button: UIButton)
And the other part of the code must be :
button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), for: .touchDown)
Thanks for helping :) Your method may be correct also but thats the one Apple wants it.

Resources