Blake Lockley
Blake Lockley

Reputation: 2961

How to find Instance of View Controller made by storyboard

I am trying to make a tabbed application in Xcode that allows the user to take a photo and edit it on the FirstViewController class and when they are done display it on the SecondViewController.

When I started the project, Xcode automatically made the two viewControllers for me in the storyboard. What I need now is to find the instance of the second viewController that was generated so I can call a method and pass an argument (the UIImage) from the first view controller to the second like this.

FirstViewContoller.m

-(void) passImageToSecondVC (UIImage *) img 
{
    [<instanceOf_SecondViewController> receiveImg: img]; 
}

SecondViewContoller.m

-(void) receiveImage (UIImage *) img 
{
    //Code to display the image received 
}

What Im asking is how can I find the name of the instance of the SecondViewController (shown by <> in the example code) generated by Xcode so I can call this method.

Although I'm very close to just doing this programmatically which I find much easier I wanna learn how to do this through the storyboard also I'm very open to hear other solutions to this problem. Thank you!

Upvotes: 0

Views: 684

Answers (2)

Bisca
Bisca

Reputation: 6763

Use delegates pattern. Make one vc be a delegate of the other vc and communicate data between them. I think It's a common scenario. https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

There's no way to do this through the storyboard. You don't access the view controller by its name. Each view controller has access to the tab bar controller through self.tabBarController. You can access individual controllers from the tab bar controller's viewControllers array. So, to get a reference to the controller in the second tab, you would use self.tabBarController.viewControllers[1].

Upvotes: 2

Related Questions