简体   繁体   English

无法将视图作为子视图添加到多个视图 - iphone dev

[英]having trouble adding a view as a subview to multiple view - iphone dev

I need to add a view to multiple views in app. 我需要在app中为多个视图添加一个视图。 I am running a function which loops through all the UIViews and add that view as subview. 我正在运行一个循环遍历所有UIViews的函数,并将该视图添加为子视图。

I have done something like this: 我做过这样的事情:

-(void) setViewForEachSegment: (UIView*)view {
    int i;
    for (i = 0; i < [segments count]; i++) {
        [[segments objectAtIndex:i] addSubview: view]
    }
}

here segments is an array of UIViews . 这里的段是UIViews的数组。

Now, what is happening is that the subview is added only to the last segment. 现在,发生的事情是子视图仅添加到最后一个段。

Has anyone else faced the same problem? 还有其他人遇到过同样的问题吗? I have been working on it for many hours, but don't know how to fix it. 我一直在研究它好几个小时,但不知道如何解决它。

Per the UIView class reference : 根据UIView类引用

A parent view may contain any number of subviews but each subview has only one superview, which is responsible for positioning its subviews appropriately. 父视图可以包含任意数量的子视图,但每个子视图只有一个超级视图,负责适当地定位其子视图。

So each time you add the view to a different parent view, it is removed from the previous parent view. 因此,每次将视图添加到不同的父视图时,都会从上一个父视图中删除它。 You will need to change your view hierarchy around to accommodate what you are trying to do, or you will need multiple subviews to add to each parent view. 您需要更改视图层次结构以适应您要执行的操作,或者您需要将多个子视图添加到每个父视图。

you can't add one view into multiple views, because a view could have single parent view at a time. 您无法将一个视图添加到多个视图中,因为视图一次可以包含单个父视图。 which is represented by superView property of your UIView . 它由你的UIView superView属性表示。

Each time Create a new object of your UIView before adding to View, 每次在添加到View之前创建UIView的新对象,

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

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