Swr79
Swr79

Reputation: 75

How to show image in SecondViewController when button is clicked in FirstViewController

After several attempts I do not succeed to understand were is the error and why the image doesn't appear... This is my code.

myProtocol.h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end

ViewController.h

#import "SecondViewController.h"

@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>
{
    UIView *view;
} 

@property (nonatomic,retain) UIImageView *imageView;

- (IBAction)sendImage:(id)sender; 

@end

ViewController.m

#import "ViewController.h" 
#import "SecondViewController.h" 
#import "myProtocol.h"

@interface ViewController () 
@end 

@implementation ViewController

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"[email protected]"]];
    [view addSubview:_imageView];
    NSLog(@"[email protected]");
}

-(UIImage *)transferImage
{
    NSLog(@"[email protected]"); 
    return _imageView.image;
} 

- (IBAction)sendImage:(id)sender 
{    
    SecondViewController *secClass = [[SecondViewController alloc] init];
    [secClass setImageName:@"[email protected]"];
    [self.navigationController pushViewController:secClass animated:YES];
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
} 

@end 

SecondViewController.h

#import <UIKit/UIKit.h> 
#import "myProtocol.h" 
#import "ViewController.h"

@interface SecondViewController: UIViewController <myProtocol, UINavigationControllerDelegate>
{
    UIView *secondView;
    IBOutlet UIImageView *myImage;
    id <myProtocol> myDelegate;
}

@property (nonatomic,retain) UIImageView *myImage;
@property (nonatomic,retain) UIImage *transferImage;
@property(nonatomic,assign) id delegate;
@property (strong, nonatomic)NSString *imageName;

-(void)callTransfer;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "ViewController.h"
#import "myProtocol.h" 

@interface SecondViewController ()
@end 

@implementation SecondViewController

@synthesize delegate, myImage, transferImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[myImage setImage:[UIImage imageNamed:_imageName]];
UIImageView * myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_imageName]];
[secondView addSubview:myImage];
}

-(void)callTransfer
{
    myImage.image=[delegate performSelector:@selector(transferImage)];
    myImage.image=[UIImage imageNamed:@"[email protected]"];
    NSLog(@"%@",myImage.image);
    NSLog(@"I am in call transfer");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

Upvotes: 1

Views: 117

Answers (3)

Matthew Hallatt
Matthew Hallatt

Reputation: 1380

Use this:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    myImage = [[UIImageView alloc] initWithImage[UIImage imageNamed:_imageName]];
    [secondView addSubview:myImage];
}

I presume you've got a variable in your interface (either in .h or .m) called myImage so when you then create the local instance of myImage (UIImageView *myImage), XCode is getting confused. As it was declared latest (no reference, sorry) it is using the local declaration, but it is aware that there is an instance variable using the same name that can't be accessed. You instead want to save straight to the instance variable by removing the UIImageView * from your method

Hope this makes sense!

Matt

Upvotes: 1

Swr79
Swr79

Reputation: 75

[secondView addSubview:myImage];

Error: Local declaration of 'myImage' hidesistance variable.

enter image description here

Upvotes: 1

Indi
Indi

Reputation: 544

It appears that you are not initializing the myImage subview before adding it in the viewDidLoad method of SecondViewController.

Try removing:

[secondView addSubview:myImage];

from viewDidLoad, and add the following to viewWillAppear:

UIImageView * myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_imageName]];

just before:

[secondView addSubview:myImage];

There may be a better way of doing this, but this might get you headed in the right direction.

Upvotes: 1

Related Questions