将视图作为子视图添加到多个视图时遇到麻烦-iPhone开发者 [英] having trouble adding a view as a subview to multiple view - iphone dev
问题描述
我需要将一个视图添加到应用程序中的多个视图.我正在运行一个遍历所有UIView并将该视图添加为子视图的函数.
I need to add a view to multiple views in app. I am running a function which loops through all the UIViews and add that view as subview.
我已经做了类似的事情:
I have done something like this:
-(void) setViewForEachSegment: (UIView*)view {
int i;
for (i = 0; i < [segments count]; i++) {
[[segments objectAtIndex:i] addSubview: view]
}
}
此处的细分是 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.
推荐答案
您不能将一个视图添加到多个视图中,因为一个视图一次只能有一个父视图.这由 UIView
的 superView
属性表示.
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
.
每次,在添加到View之前,先创建 UIView
的新对象
Each time Create a new object of your UIView
before adding to View,
这篇关于将视图作为子视图添加到多个视图时遇到麻烦-iPhone开发者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!