Search code examples
iosobjective-cuiviewcontrollerviewwillappeartouch-up-inside

Objective-C: ViewWillAppear runs before body of buttonTouchUpInside


I have a Storyboard iOS Application with 2 view controllers-

The first Controller has a UIButton with an outlet to the next view controller and a buttonTouchUpInside method.

At the moment, the second view controller's lifecycle begins before the body of buttonTouchUpInside runs - which is bad news because viewWillAppear relies on the body of buttonTouchUpInside being completed. How do I stop this from happening?

Thanks in Advance


Solution

  • I'm assuming by "outlet to the next view controller" you mean an action segue to display the next view controller, and yes, this is correct behavior in storyboards.

    If you want to perform an IBAction before performing the segue, you should define a manual segue between the two view controllers without referencing the button, give it an identifier in IB, and then in the IBAction method definition for your first view controller, use this code:

    [self performSegueWithIdentifier:@"segueIdentifierGoesHere" sender:self];