1
votes

My task is to set background image in UIImageView in SecondViewController by clicking button on the FirstViewController.

FirstViewController.h :

 #import <UIKit/UIKit.h>
 #import "SecondViewController.h"

 @class SecondViewController;

 @interface ViewController : UIViewController
 {
    SecondViewController *secondViewData;
 }

 // pointer to second view 
 @property (nonatomic, strong) SecondViewController *secondViewData;

 // buttons
 - (IBAction)changeBack:(id)sender;

 @end

button method in FirstViewController.m :

- (IBAction)changeBack:(id)sender {
    SecondViewController *svc = [[SecondViewController alloc] init];

    self.secondViewData = svc;

    svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

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

SecondViewController.h :

 #import <UIKit/UIKit.h>
 #import "FirstViewController.h"

 @interface SecondViewController : UIViewController

 @property (strong, nonatomic) IBOutlet UIImageView *back;

 @end

back - it's a IBOutlet UIImageView in SecondViewController. Also I imported FirstViewController.h in SecondViewController.m. I tried to pass string to SecondViewController - and it's logged fine in -ViewDidLoad in SecondViewController, but can't set UIImageView.

4
What do you mean by "back- it's a UIImageView in SecondViewController"? Is back an IBOutlet. If not, then it would be helpful to see any code that has to do with "back" in SecondViewController.rdelmar
good approach to transfer data between views - iphonedevsdk.com/forum/iphone-sdk-development/…ignotusverum
Is this an ARC project? Is back setup in the xib of SecondViewController? Even though you're setting back pragmatically it would be overwritten when the view loads in svc by any default values. The best way may be to pass the image as a value in a custom init method and load it in the svc viewDidLoad method.propstm
Rather than setting the imageview for back, I'd suggest having a property for the image (not the imageview) and have the viewDidLoad of SecondViewController set the imageview to use the image property you set here. Generally, you shouldn't be messing around with controls before viewDidLoad since the view for the view controller probably hasn't even been created yet.Rob
rdelmar, yes back it's an IBOutlet< please check edited questionAlex

4 Answers

2
votes

First of all, read Resource Management in View Controllers

I thought, the problem is that first you set UIImageView through property, and then its set from xib, when view loaded in modal controller.

1
votes

This line is the problem:

svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];

You're setting back to a newly allocated image view, rather than the one that it's hooked up to in IB. Just change that line to:

svc.back.image = [UIImage imageNamed:@"1.png"]];
1
votes

You can do something like this. You can have an an outlet of UIImageView in your secondviewcontroller.h

        IBOutlet UIImageView *imgView;

also property and synthesize this

       @property(nonatomic, retain)  IBOutlet UIImageView *imgView;  

connect the outlet iof secondViewController in the FirstViewController.xib

In your firstviewcontrollem.m implement this where you want to set the image in the secondViewController UIImageView

    self.secondViewData.imgView.image = [UIImage imageNamed:@"1.png"];
1
votes

U need to take another imageview at second view controller.I named as imgView.Let original image view as bgView.

enter code here

//At secondviewcontroller.h

@property(nonatomic,strong)IBOutlet UIImageView *bgView;

@property(nonatomic,strong)IBOutlet UIImageView *imgView;

//At secondviewcontroller.m

//At viewDidLoad

self.bgView.image=self.imgView.image;

//At firstviewcontroller.m

//button action should be like this

-(IBAction)buttonPressed:(id)sender {

SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

secondVC.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hue.png"]];

[self presentModalViewController:secondVC  animated:YES];

}

Try this.