图像没有从一个视图传递到另一个视图

时间:2013-11-28 04:40:12

标签: ios objective-c uiviewcontroller

我正在制作贺卡应用。在第一个视图中,用户单击图像的按钮,这将他带到下一个视图,并将该图像设置为背景图像。但是当我按下按钮时,它会转到下一个视图,但不会显示图像。我哪里错了?

ViewController.h (第一次观看)

@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 (第二视图)

@interface GreetingCard : UIViewController {

    IBOutlet UIImageView *background;
    UIImageView *passedImage;
}

@property (nonatomic , retain) UIImageView *passedImage;

@end

GreetingCard.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    background.image = passedImage.image;
}

3 个答案:

答案 0 :(得分:2)

您应该将UIImage传递给新的视图控制器,而不是UIImageView。 UIImageView是您在Storyboard中的视图,基于您将其声明为IBOutlet的事实。您正在尝试传递故事板中引用的UIImageView。

您需要将实际图像传递给新控制器,然后使用该图像设置背景图像。像这样:

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

然后在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];

}

然后在GreetingCard的viewDidLoad中

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

此外,如果您使用的是Storyboard,您可能需要考虑使用segues而不是从代码中以模态方式显示视图。您可能还想将GreetingCard命名为GreetingCardViewController以避免混淆,但这只是一种风格。你通常不应该命名一个UIImageView"图像"因为这会导致混乱并导致像这里的错误。

您可能还需要考虑使用ARC(自动参考计数)。

答案 1 :(得分:1)

您正在传递 UIImageView 而不是 UIImage 。当您将 UIImageView 传递给另一个类时,图像将不会随之传递。所以你必须传递 UIImage 并使用这个 UIImage 来设置为背景图像

所以你必须声明另一个对象。像这样改变你的代码

@interface GreetingCard : UIViewController {

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

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

@end

将图像传递给 UIImage 对象

theme.passedImage = image.image;
在GreetingCard.m中

- (void)viewDidLoad
{
    [super viewDidLoad];

    background.image = passedImage;
}

答案 2 :(得分:-1)

在第二个视图中使用了UIImage *passedImage;而不是UIImageView

相关问题