0

I can't figure out what's wrong with this. I am trying to add a sub view to my current view. I alloc and initWithNibName my SecondViewController and set its myImageView parameter that is a UIImageView. The problem is that when the subView is added the myImageView is not set.

This is the code:

- (void)viewDidLoad
{
   [super viewDidLoad];
SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

secondView.myImageView.image = [UIImage imageNamed:@"image1.png"];

[self.view addSubview secondView.view];
}

if I set an image to myImageView via Interface Builder it is correctly displayed on addSubview but if I set the property as described above it doesn't work... The UIImageViewoutlet is correctly connected on IB.

this is the SecondViewController :

@interface SecondViewController : UIViewController
{

}

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


@end
Lucas Pereira
  • 569
  • 1
  • 11
  • 23

2 Answers2

1

I believe your problem is that when you call secondView.myImageView.image = [UIImage imageNamed:@"image1.png"]; the myImageView outlet has not yet been set. The documentation for initWithNibName:bundle: says, "The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed." So you need code like this:

- (void)viewDidLoad
{
   [super viewDidLoad];
SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

// Force the nib to load, which subsequently sets the myImageView outlet
[secondView view];

secondView.myImageView.image = [UIImage imageNamed:@"image1.png"];

[self.view addSubview secondView.view];
}

However, I don't recommend this approach. You should really set the image of the myImageView outlet in SecondViewController's -viewDidLoad method. That's where it belongs.

// SecondViewController.m

- (void)viewDidLoad
{
     [super viewDidLoad];

     self.myImageView.image = [UIImage imageNamed:@"image1.png"];
}

And then in the other view controller's -viewDidLoad method, just add SecondViewController's view as a subview like before:

- (void)viewDidLoad
{
     [super viewDidLoad];

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

     [self.view addSubview:secondVC.view];
}
EJV
  • 1,009
  • 1
  • 10
  • 17
0

If you set it myImageView via IB it will instantiate myImageView. IF you use the above code, myImageView is in instantiated.

You have to add the following line in SecondViewController viewDidLoad or initWithFrame:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)]
secondView.myImageView = imageView;

[Thanks H2CO3]

James Paolantonio
  • 2,194
  • 1
  • 15
  • 32
  • No. You rather have to add `self.imageView = [[UIImage alloc] init];` in SecondViewController - your approach breaks encapsulation. –  Aug 01 '12 at 18:24
  • In IB I added a `UIImageView` and connected it's Outlet to `myImageView`. Do I still have to alloc and initialize it? – Lucas Pereira Aug 01 '12 at 18:24
  • I tried doing this: `secondView.myImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image1.png"]];` but it didn't change a thing. =/ – Lucas Pereira Aug 01 '12 at 18:26