Ajeet
Ajeet

Reputation: 143

Pass image from one view to another view by preparesegue?

I am trying to pass image from one viewcontroller to another . I have tried all available solutions on stackoverflow , still i am struggling with the same. How can i pass uiimage through preparesegue ??

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ( [segue.identifier isEqualToString:@"filter"]) {
    FilterViewController *cvc = [segue destinationViewController];
    UIImage *image = imageView.image;
    cvc.desktopview.image = image;
    NSLog(@"segue");
}

}

Upvotes: 0

Views: 3862

Answers (4)

Adnan Aftab
Adnan Aftab

Reputation: 14477

ViewDidLoad is called only after ALL outlets are loaded, so never try to set outlet/views value in prepare segue method, as viewDidLoad will be called after prepare segue method. Now you can do one that create an UIImage property in filterViewController and set its value. and In viewDidLoad method set it to specfic imageView.

Upvotes: 1

codercat
codercat

Reputation: 23271

Optimized way

 [segue.destinationViewController setAnotherImage:yourImage];

Another view controller

-(void)setAnotherImage:(UIImage *)happiness
{
    //get your image here
}

Upvotes: 0

Desdenova
Desdenova

Reputation: 5378

You can't do this because it isn't ready yet.

cvc.desktopview.image = image;

Instead you need to store it in a UIImage property and use it in viewDidLoad method of your destination view controller.

Something like this in prepareForSegue

cvc.image = image;

and in the view controller

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.desktopview.image = image;


}

Edit:

You need this at your view controller's header file.

@property (nonatomic, strong) UIImage * image;

Upvotes: 1

AlgoCoder
AlgoCoder

Reputation: 1696

Go to FilterViewController.h

add a uiimage there @property(nonatomic,strong)UIImage *dvImage;

Go to FilterViewController.m

Synthesize it @synthesize dvImage;

Go to the code Where you are calling the FilterViewController

FilterViewController *cvc = [segue destinationViewController];
UIImage *image = imageView.image;
cvc.setDvImage = image;
NSLog(@"segue");

and then in viewDidLoad set self.desktopview.image = dvImage;

Don't forgot to check the segue identifier

Hope this helps

Upvotes: 1

Related Questions