[英]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. 我正在尝试将UIImageView子视图添加到我的viewController.view中,考虑它们是在图像上方还是在下方。
Basically, my viewController has an image of a body. 基本上,我的viewController具有身体的图像。 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: 我想我可以尝试在UIController上创建一个UIImageView属性作为参考点,并执行以下操作:
//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. 但是,此代码不起作用,因为我收到警告"Property body not found on object of type 'UIViewController *'"
,即使我@property和@synthesize,也是如此。
Does anyone know what I'm doing wrong, or have a better design on how to implement this? 有谁知道我在做什么错,或者对如何实现这个有更好的设计? Thanks 谢谢
@shubhank: @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]
. [self.view insertSubview:sprite belowSubview:body]
。 Don't insert viewController
after self
, 'cause self
IS viewController
. 不要在self
之后插入viewController
,因为self
是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 在这里,您通过self.viewController.body访问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. 因此,如何通过视图控制器和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. 该属性保留UIImage视图。您应该释放它。
Problem Solved: 问题解决了:
Silly issue. 愚蠢的问题。 I simply needed to forward declare my ViewController class in my MenuItem class, rather than use UIViewController *. 我只需要在MenuItem类中向前声明我的ViewController类,而不是使用UIViewController *。
Thanks for your help. 谢谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.