Simon de Wit
Simon de Wit

Reputation: 13

Passing images between View Controllers

First of all, I'm new to XCode and programming and my English is not perfect. I have 2 view controllers, L1ViewController and ImageViewController. In the L1ViewController i have several buttons with an image as background. What I'm trying to do is when you click a button, the action passes a password and an image to the Image View controller. In the ImageViewController I have a UITextfield and a UIImageView. Passing the password string works fine. But passing the image seems harder than I thought. The UIImageView doesn't display the image.

In my L1ViewController.h I've created a

    -(IBAction)img1; 

In my L1ViewController.m this is the code to pass the image and the passwordstring:

    -(IBAction)img1;
{
ImageViewController *img = [[ImageViewController alloc] initWithImage:[UIImage imageNamed:@"myimage.jpg"] AndAnswerString:@"mypassword"];   
[self presentModalViewController:img animated:NO];
}

In my ImageViewController I have this code to receive the string and the image:

-(id)initWithImage:(UIImage *)inImage AndAnswerString:(NSString *)inAnswerString
{
    if (self = [super init])
    {
        [inImage release];
        [self setImage:inImage];
        [self setAnswerString:inAnswerString];

    }
    return self;
} 
-(void)setImage:(UIImage *)inImage
{
[inImage release];
[logoField setImage:inImage]; 
}

-(void)setAnswerString:(NSString *)inAnswerString
{
answerString = inAnswerString;
}

In my ImageViewController.h I've

@interface ImageViewController : UIViewController { 
IBOutlet UITextField *answerField;
IBOutlet UIImageView *logoField;

NSString *answerString;
 }

@property (nonatomic, retain)NSString *logo;
-(id)initWithImage:(UIImage *)inImage AndAnswerString:(NSString *)inAnswerString;
-(void)setImage:(UIImage *)inImage;
-(void)setAnswerString:(NSString *)inAnswerString;

-(IBAction)enterAnswer;
-(IBAction)back;

Why is my answerstring working fine but shows my ImageViewController no image? Again, I'm new to XCode, maybe it's just a stupid fault. Thanks in Advance!

Upvotes: 1

Views: 2386

Answers (3)

polarware
polarware

Reputation: 2519

ok ..I think you have to pass it as an id and not as UIImage witch doesn't work: in your secondUIViewController.h:

@property (nonatomic, strong) id image;

in secondViewController.m

@synthesize image = _image;

Upvotes: 0

Fevicks
Fevicks

Reputation: 223

You have release "inImage" - before you have used,so remove the code "[inImage release];" from all the functions, Release it after you have use that image

so the new code be.. the following

   -(id)initWithImage:(UIImage *)inImage AndAnswerString:(NSString *)inAnswerString
{
if (self = [super init])
{
    //[inImage release];
    [self setImage:inImage];
    [self setAnswerString:inAnswerString];

}
return self;
} 
-(void)setImage:(UIImage *)inImage
{
[logoField setImage:inImage]; 
[inImage release];
}

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31016

When your view controller is being initialized, the views have not been loaded yet. (That happens just before your viewDidLoad: method is called.) Saving the string works because you're not trying to put it directly into answerField but, with the image, you're trying to update logoField too early.

If the text is working, you must have code somewhere that assigns answerString to answerField.

Upvotes: 1

Related Questions