【问题标题】:xcode add a class/object to a subviewxcode将类/对象添加到子视图
【发布时间】:2011-07-11 11:53:01
【问题描述】:

希望您能提供帮助。我基本上需要向 UIView 中的 UIView 添加一个对象。

在界面生成器中我有我的视图,在里面我有另一个视图,里面有一个图像(地图)(全部由界面生成器完成)。第二个视图我使用了一个名为“MainGameMapViewController”的类(原因是第一个视图是静态的/包含文本/分数等,里面的视图是游戏,整个视图可以用“MainGameMapViewController”中的代码拖动")。

我需要将对象添加到“MainGameMapViewController”UIView。

我添加了一个名为“CharMain”的类,并在 MainViewController 中使用了这段代码:

-(void) viewWillAppear:(BOOL)animated {

    CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
    [self.view addSubview:charMain];

}

CharMain 包含绘制矩形并添加图像的代码,这工作正常,但需要将其添加到第二个视图“MainGameMapViewController”。

我尝试在 MainGameMapViewController 文件中添加代码,它显示“error: request for member 'view' in something not a structure or union”,我尝试过 [self addSubview:charMain];

这没有错误,但图像没有出现,我如何让它出现在第二个 UIView 中?谢谢!

【问题讨论】:

    标签: xcode class uiview subview


    【解决方案1】:
    CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
    //this should be your subview instead of self.view try adding it to your subview's instance.
        [MainGameMapViewControllerInstance.view addSubview:charMain];
    

    【讨论】:

    • 谢谢,那我也要加这个“MainGameMapViewController”类吗?我尝试将其添加到 MainGameViewController 和“MainGameMapViewController”中,但出现错误:MainGameMapViewControllerInstance。我把它改成了 ** MainGameMapViewController** ,还是报错。
    • MainGameMapViewController 是我假设是 UIViewController 的子类。所以 MainGameMapViewController 有它自己的视图,你需要将你的 charMain 添加到那个视图中。
    • 好吧,我通过大量的反复试验解决了这个问题!思想 id 发布并让其他人知道。在 MainGameViewController 中,我刚刚添加了 IBOutlet UIView mapView;进入 .h 并将第二个 UIView 链接到该插座。并在 .m 中将代码更改为: [self.**mapView* addSubview:charMain];谢谢
    • 如果您仍然无法执行此操作,请将您的代码通过邮件发送给我。我会更新的。
    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多