Reputation: 69
I'm trying to pass a UIImage from a First View Controller to Second View Controller, but this doesn't appear in Second View. Why?
My code in FirstController.m:
SecondController *second = (SecondController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"];
second.imgCard.image = [UIImage imageNamed:@"image.png"];
[self.navigationController pushViewController:second animated:YES];
My code in SecondController.h:
@property (nonatomic, strong) IBOutlet UIImageView *imgCard;
My code in SecondController.m:
@implementation SecondController
@synthesize imgCard;
Upvotes: 0
Views: 97
Reputation: 119252
imgCard
will be nil
until the second view controller's view has been loaded. This doesn't happen until you push the view controller. A view controller's view isn't created, and the outlets aren't connected, until needed.
You have two options.
pushViewController
call (not sure if that will cause a visual glitch)image
property to SecondController
and set that. In viewDidLoad
of SecondController
, set imgCard.image = self.image;
. Upvotes: 2
Reputation: 745
If image that you want to set on your ImageView is in your Assets.xcassets
or in your project folder ,than you don't need to pass image in prapare for sague just use as below;
imgCard.image = [UIImage imageNamed:@"image.png"];
in FirstController.m:
SecondController *second= (SecondController*)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"];
second.imageVar = [UIImage imageNamed:@"image.png"];
[self.navigationController pushViewController:second animated:YES];
in SecondController.h:
@property (nonatomic, strong) IBOutlet UIImageView *imgCard;
@property UIImage *imageVar;
in SecondController.m:
@implementation SecondController
@synthesize imageVar,imgCard;
-(void)viewDidLoad{
[super viewDidLoad];
imagCard.image = imageVar;
}
Upvotes: 3
Reputation: 1235
Your first line of code is wrong. The correct way to init your second view controller is :
SecondController *second = (SecondController*)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"];
second.imgCard.image = [UIImage imageNamed:@"image.png"];
...
Upvotes: 1