0

I'm trying to pass a UIImage value to a UIImageView inside a View that I'm adding as a subView.

Here's what I'm doing in my mainViewController:

- (IBAction)selectStore:(id)sender {

    StoreSelectorViewController *SSVC = [[StoreSelectorViewController alloc] initWithNibName:@"StoreSelectorViewController" bundle:nil];
    [self.navigationController addChildViewController:SSVC];
    [SSVC.backgroundView setImage:[UIImage imageNamed:@"CoolBG.png"]];
    [self.view.window addSubview:SSVC.view];
}

I did the following in my StoreSelectorViewController.h file:

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

And I also linked it in the XIB file.

Everything loads, except that I never receive the UIImage value. Thanks

KingPolygon
  • 4,753
  • 7
  • 43
  • 72
  • Is `selectStore:` getting called? You have it set as an `IBAction`, but are you sure you wired it in correctly in Interface Builder? Perhaps put a `NSLog` statement in that message to ensure it's called. – greg Jan 11 '14 at 01:29
  • Hey Greg! Yeah In fact I actually have a Button in StoreSelectorViewController, which does appear on the screen, and then when I click it, it removes the whole view. So it seems like the problem has to do specifically with the uiimageview. Let me try a few NSLogs. – KingPolygon Jan 11 '14 at 01:32
  • Also, this looks a bit odd. You're adding a subview to the `UIWindow`, not the current `UIView`, but you're also adding another `UIViewController` to the `UINavigationController`. Are you sure you don't mean to `[self.navigationController pushViewController]`, and not add the subview or CVC? – greg Jan 11 '14 at 01:32
  • I'm trying to add the subView in front of the navigationController that MainViewController is in, that's why I'm trying to add it to the UIWindow, rather than the ViewController's view. – KingPolygon Jan 11 '14 at 01:33
  • I have updated the ans please follow and make it correct. If it really helps – Hussain Shabbir Jan 11 '14 at 01:42

3 Answers3

0

Sometimes using this doesn't seem to work:

[SSVC.backgroundView setImage:[UIImage imageNamed:@"CoolBG.png"]];

So instead I use this two line version, in a slightly dif't order, setting the image, before displaying its parent (SSVC):

StoreSelectorViewController *SSVC = [[StoreSelectorViewController alloc]     initWithNibName:@"StoreSelectorViewController" bundle:nil];
UIImage *img = [UIImage imageNamed:@"CoolBG.png"];
[SSVC.backgroundView setImage:img];
[self.navigationController addChildViewController:SSVC];
[self.view.window addSubview:SSVC.view];
Fluffhead
  • 845
  • 1
  • 9
  • 21
0

The problem is in this line [self.view.window addSubview:SSVC.view]; So just replace the line to below:-

Also you want to add imageview as a subview of view. So use below code where backgroundView is the imageview:-

[SSVC.view addSubview:SSVC.backgroundView];
[self.view addSubview:SSVC.view];. 
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
0

You really shouldn't be calling addChildViewController regardless, it's not what you want. You're not making use of the UIViewController either, just stealing its view property. It's possible that because of this, the UIViewController is not fully instantiated since it's not presented (viewWill/Did) methods never get called, and so backgroundView is probably nil.

greg
  • 4,843
  • 32
  • 47