as diu
as diu

Reputation: 1090

How to call IBAction of another UIViewController soon after presentViewController?

So currently the user is in ViewController2, to transition to ViewController1 the presentViewController is being called.

Soon after ViewController1 opens up, there is an IBAction method that needs to be called.

How can this be accomplished? Any help would be greatly appreciated. Thank you!

Upvotes: 0

Views: 194

Answers (1)

Mitchell Currie
Mitchell Currie

Reputation: 2809

The view controller can call it itself in either viewWillAppear or viewDidAppear.

self.myAction()

If the view controller is exclusively used in the context described you could do it unconditionally, otherwise if you need to do it conditionally - expose a Boolean:

public var doActionAfterAppear = false

public override viewDidAppear(animated isAnimated: Bool) {
    super.viewDidAppear(animated: animated)
    if self.doActionAfterAppear {
        self.myAction()
        self.doActionAfterAppear = false
    }
}

And lastly before you present the second view controller or transition to it:

nextViewController. doActionAfterAppear = true

Upvotes: 1

Related Questions