繁体   English   中英

插入其他类的子视图

[英]Inserting subviews from other classes

我正在尝试将UIImageView子视图添加到我的viewController.view中,考虑它们是在图像上方还是在下方。

基本上,我的viewController具有身体的图像。 我允许用户在场景中添加配件,例如帽子。 我需要将这些图像中的一些放在身体视图索引的下方。

但是我在弄清楚如何在身体后面添加图像时遇到了麻烦。 我想我可以尝试在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];

但是,此代码不起作用,因为我收到警告"Property body not found on object of type 'UIViewController *'" ,即使我@property和@synthesize,也是如此。

有谁知道我在做什么错,或者对如何实现这个有更好的设计? 谢谢

@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之后插入viewController ,因为selfviewController

有两件事正在发生,我似乎无法理解。

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

在这里,您通过self.viewController.body访问body

然后在哪里定义主体

[self.view addSubview:body]

因此,如何通过视图控制器和self.view都可以访问它。

这里有问题。

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

更改为

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

该属性保留UIImage视图。您应该释放它。

问题解决了:

愚蠢的问题。 我只需要在MenuItem类中向前声明我的ViewController类,而不是使用UIViewController *。

谢谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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