Reputation: 1477
I'm trying to have my app take a picture then pass the image onto another view for editing but I can't seem to figure out how to change views, how to add an "ID" to a view in the storyboard or how to pass data between views.
Upvotes: 1
Views: 1566
Reputation: 5237
The communication between two UIViewControllers needs to be managed manually, however, if you are using storyboards to create your app, there's some things that you need to take into account.
Let's say you have FirstViewController and SecondViewController(Lets assume you have everything set up in your Storyboard). FirstViewController will pass a UIImage to SecondViewController and they will look something like this.
@interface FirstViewController : UIViewController
- (IBAction)transitionToNextViewController;
@property (retain, nonatomic) UIImage *image;
@end
@implementation FirstViewContoller
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:@"SegueIdentifier"];
}
@end
And:
@interface SecondViewController : UIViewController
@property (retain, nonatomic) UIImage *image;
@end
You are probably wondering how are you supposed to pass the image to the SecondViewController. Well, when using storyboards, your UIViewControllers will receive a call to their method prepareForSegue:sender: . All you have to do is set the image property for the second UIViewController there.
@implementation FirstViewController
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:@"SegueIdentifier"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController; // You have to cast it
secondViewController.image = self.image;
}
@end
and that's it. To better understand Storyboards please read the apple docs here.
Upvotes: 1