UIView:如何找出一个视图是否已经存在? [英] UIView: How to find out if a view is already exists?

查看:190
本文介绍了UIView:如何找出一个视图是否已经存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何找出一个子视图(在我的case pageShadowView)已经添加到我的视图。

I was wondering how to find out if a subview (in my case pageShadowView) has already been added to my view.

我想出了这一点,但它不工作:

I've come up with this, but it doesn't really work:

if ([pageShadowView isKindOfClass:[self.view class]]) {
        [self.view addSubview:pageShadowView];
    }

此外,我还是对自我的东西感到困惑。我知道这有关清楚,我们正在谈论当前ViewController的视图...但我真的需要它,如果有(1)没有其他ViewControllers或(2)如果它没有真正重要因为如果我想引用另一个viewController,我一定要调用它?

Also, I'm still confused about the self.-thing. I know that this has to do with making clear that we are talking about the view of the current ViewController ... but do I really need it if there (1) are no other ViewControllers or (2) if it doesn't really matter because if I ever wanted to refer to another viewController, I'd make sure to call it?

对不起,如果这一切都很基础,但我非常感谢您的意见。

I'm sorry if this is all very basic, but I'd be very grateful for your comments.

推荐答案

这里:

BOOL doesContain = [self.view.subviews containsObject:pageShadowView];

是的,你需要这个 self 。在 UIViewController 上没有明确的ivarview。 self.view 语句实际上是对方法 [self view] 的调用,它是 UIViewController 的视图。

And yes, you need this self. There is no explicit ivar "view" on UIViewController. The self.view statement is actually a call on method [self view] which is a getter for UIViewController's view.

这篇关于UIView:如何找出一个视图是否已经存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆