简体   繁体   中英

Inserting subviews from other classes

I'm trying to add UIImageView subviews to my viewController.view, with consideration for whether they go above or below an image.

Basically, my viewController has an image of a body. I allow the user to add accessories to the scene, for example a hat. I need to put some of these images above, some below the body view index.

However I'm having trouble figuring out how to add the image behind the body. I figured I could try and create a UIImageView property as a reference point on the viewController and do something like this:

 //this is from my menuItem class which has a UIViewController property connecting it to the main scene
[self.viewController.view insertSubview:sprite belowSubview:self.viewController.body];

However this code doesn't work, as I get the warning "Property body not found on object of type 'UIViewController *'" , even though I @property and @synthesize it.

Does anyone know what I'm doing wrong, or have a better design on how to implement this? Thanks

@shubhank:

  //.h
@interface ViewController : UIViewController {
    UIImageView *body;
}
@property (nonatomic, retain)  UIImageView *body;

 //.m (in viewDidLoad)
 self.body = [[UImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)];
    self.body.image = [UIImage imageNamed:@"body.png"];
    [self.view addSubview:body];

[self.view insertSubview:sprite belowSubview:body] . Don't insert viewController after self , 'cause self IS viewController .

there is two things happening that i can't seem to understand.

[self.viewController.view insertSubview:sprite belowSubview:self.viewController.body]

Here you are accessing body by self.viewController.body

and then where body is defined

[self.view addSubview:body]

so how come you can access it both by view controller and self.view.

There is something wrong here..

Also

 self.body = [[UIImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)];

change it to

 self.body = [[[UIImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)]autorelease];

the property retains the UIImage view..you should release it.

Problem Solved:

Silly issue. I simply needed to forward declare my ViewController class in my MenuItem class, rather than use UIViewController *.

Thanks for your help.

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