I have an iPad application that I am working on where, from my main viewController (call it mainViewController), I call and display another viewController (call it nextViewController) that is created from an .xib file. This second viewController allows the user to capture an image like so:
- (IBAction)imageCapture:(id)sender {
_myImage = [_nextView captureImage];
UIImageWriteToSavedPhotosAlbum(_myImage, nil, nil, nil);
[self dismissViewControllerAnimated:YES completion:nil];
}
Once this image is captured, I need this image to now be passed back to the calling viewController (mainViewController), but I honestly am not sure how to do this. I have tried to create a property reference of my mainViewController in my nextViewController, which I was trying to do in order to pass the reference of the newly acquired image to an attribute of the mainController, but this is not working.
Thanks in advance to all who reply.
To do this effectively, you should not make a property
mainViewController
in the presented view controller. This would cause reusability issues, plus it's tight coupling which is not desirable. Instead, as the comments above note, you should use a protocol. For Objective-C, see this documentation for protocol syntax.In your NextViewController, you should make a delegate method something like this:
Then, in your main view controller class, you should implement this method and do whatever you want with the image. Notably, you may want to dismiss the controller from the main view controller. Don't forget to set the delegate property of the next view controller when you create it.
I hope this clarifies your problem!
Firstly, you should create a property in
secondViewController
to pass yourmainViewController
to before present the secondViewController. Ex:Secondly, you will need a property in
mainViewController
to pass the image back fromsecondViewController
. Ex:Hopefully this will help you.