简体   繁体   中英

Passing a UIImage to a SubView's UIImageView?

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

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];

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];. 

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM