简体   繁体   中英

Image not passing from one view to another

I am making a greeting card app. In the first view the user clicks on a button of an image, which takes him to the next view with that image set as the background image. But when I press the button, it does go to the next view but the image is not displayed. Where am I going wrong?

ViewController.h (First View)

@interface ViewController : UIViewController {

    GreetingCard *theme;
    IBOutlet UIImageView *image;
}

@property (nonatomic , retain) GreetingCard *theme;

- (IBAction)bday1;

@end

ViewController.m

@implementation ViewController
@synthesize theme;

- (IBAction)bday1 {

    UIViewController *greeting = [self.storyboard instantiateViewControllerWithIdentifier:@"GreetingCard"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:greeting];

    self.theme = greeting;    
    theme.passedImage = image;

    [self presentViewController:navigationController animated:YES completion:nil];
}

GreetingCard.h (Second View)

@interface GreetingCard : UIViewController {

    IBOutlet UIImageView *background;
    UIImageView *passedImage;
}

@property (nonatomic , retain) UIImageView *passedImage;

@end

GreetingCard.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    background.image = passedImage.image;
}

You should be passing the UIImage to the new view controller, instead of the UIImageView. The UIImageView is the view that is in your Storyboard based on the fact that that you declared it as an IBOutlet. You are trying to pass the UIImageView that is referenced in the storyboard.

You will need to pass the actual image to the new controller and then set the background's image with that image. Like so:

ViewController.h

@interface ViewController : UIViewController {

GreetingCard *theme;
IBOutlet UIImageView *imageView;

}

@property (nonatomic , retain) GreetingCard *theme;

- (IBAction)bday1;

@end

GreetingCard.h

@interface GreetingCard : UIViewController {
    IBOutlet UIImageView *background;
    UIImage *passedImage;
}

@property (nonatomic , retain) UIImage *passedImage;

@end

Then in ViewController.m

@implementation ViewController
@synthesize theme;

- (IBAction)bday1 {

UIViewController *greeting = [self.storyboard instantiateViewControllerWithIdentifier:@"GreetingCard"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:greeting];

self.theme = greeting;    
self.theme.passedImage = imageView.image;

[self presentViewController:navigationController animated:YES completion:nil];

}

Then in viewDidLoad of GreetingCard

- (void)viewDidLoad
{
    [super viewDidLoad];
    background.image = passedImage;
}

Also if you are using Storyboard, you may want to consider using segues instead of presenting the view modally from the code. You may also want to name your GreetingCard as a GreetingCardViewController to avoid confusion, but that is just a style thing. You should in general not name a UIImageView "image" because this can cause confusion and lead to the bugs like the one here.

You may also want to consider using ARC (Automatic Reference Counting).

You are passing UIImageView not UIImage . When you are passing UIImageView to another class, the image will not passed along with it. So you have to pass the UIImage and use this UIImage to set as background image

So you have to declare another object. Change your code like this

@interface GreetingCard : UIViewController {

IBOutlet UIImageView *background;
UIImage *passedImage;
UIImageView *passedImageView;
}

@property (nonatomic , retain) UIImage *passedImage;
@property (nonatomic , retain) UIImageView *passedImageView;

@end

pass the image to this UIImage object as

theme.passedImage = image.image;

in GreetingCard.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    background.image = passedImage;
}

In second view used UIImage *passedImage; instead of UIImageView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM