David David - 6 months ago 24
iOS Question

OBJECTIVE-C: PushViewController with UIImage doesn't work

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;

Answer

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;
}