Search code examples
swiftinvokeibactionviewdidload

How to invoke an extension for UIButton in viewDidLoad


I am still new to Swift and programming in general. I am currently stuck on how to make an extension on a button self trigger(invoke) in viewDidLoad. my code works when I tap the button but I want this do self trigger without tapping in viewDidLoad.

@IBOutlet weak var blinkButton: UIButton!



// Yellow Colour Error: All paths through this function will call itself

@IBAction func blinkButtonTapped(_ sender: UIButton) {
       blinkButtonTapped(blinkButton)
    }



// Yellow Colour Error: All paths through this function will call itself

extension UIButton {

    func blinkButton(_ sender: AnyObject?) {
        self.blinkButton( nil )
        let flash = CABasicAnimation(keyPath: "opacity")
        flash.duration = 0.5
        flash.fromValue = 1
        flash.toValue = 0.1
        flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        flash.autoreverses = true
        flash.repeatCount = 100000000

        layer.add(flash, forKey: nil)


    }

I want to call blinkButton function in ViewDidLoad.


Solution

  • You need to pass value as nil, because parameter has optional type or you can pass any object type.

    override func viewDidLoad() {
       super.viewDidLoad()
           blinkButton(nil) 
    }
    

    And you need to remove in self.blinkButton(nil) in func blinkButton, it's called two time.