n.e*_*ind 24 iphone cocoa-touch objective-c uiview
我想知道如何找出子视图(在我的情况下是pageShadowView)是否已添加到我的视图中.
我想出了这个,但它确实不起作用:
if ([pageShadowView isKindOfClass:[self.view class]]) {
[self.view addSubview:pageShadowView];
}
Run Code Online (Sandbox Code Playgroud)
而且,我仍然对自我感到困惑.我知道这与明确我们正在谈论当前ViewController的视图有关...但如果(1)没有其他ViewControllers或(2)如果它真的不重要,我真的需要它吗?因为如果我想引用另一个viewController,我一定要调用它吗?
如果这一切都非常基本,我很抱歉,但我非常感谢你的评论.
Bar*_*ski 84
这里:
BOOL doesContain = [self.view.subviews containsObject:pageShadowView];
Run Code Online (Sandbox Code Playgroud)
是的,你需要这个self.没有明确的ivar"视图" UIViewController.该self.view语句实际上是一个方法调用,[self view]它是一个getter for UIViewControllerview.
con*_*are 24
给它一个唯一的标签:view.tag = UNIQUE_TAG,然后检查容器视图是否存在:
BOOL alreadyAdded = [containerView viewWithTag:UNIQUE_TAG] != nil;
Run Code Online (Sandbox Code Playgroud)
你可以找到像这样的子视图
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIView class]])
{
//here do your work
}
}
Run Code Online (Sandbox Code Playgroud)
在Swift中还有一种方法可以找到:isDescendant(of view: UIView) -> Bool或者在Obj-C中: - (BOOL)isDescendantOfView:(UIView *)view
斯威夫特:
if myView.isDescendant(of: self.view) {
//myView is subview of self.view, remove it.
myView.removeFromSuperview()
} else {
//myView is not subview of self.view, add it.
self.view.addSubview(myView)
}
Run Code Online (Sandbox Code Playgroud)
Obj-C:
if([myView isDescendantOfView:self.view]) {
//myView is subview of self.view, remove it.
[myView removeFromSuperView];
} else {
//myView is not subview of self.view, add it.
[self.view addSubView:myView];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33634 次 |
| 最近记录: |