开发者

iOS SDK Check if view already exists

开发者 https://www.devze.com 2023-02-07 10:36 出处:网络
I have a UIWebView that is drawn programmatically and gets allocated and displayed through multiple subviews (the webview gets added to the superview).

I have a UIWebView that is drawn programmatically and gets allocated and displayed through multiple subviews (the webview gets added to the superview).

This all works, however I have one little problem:

If 2 different subviews display this webview then I get 2 webviews, so when 1 view dismisses the webview the other remains. I don't want this.

Originally I was thinking just implement the webview in the 开发者_JAVA技巧superview class, however it didn't work.

How can I have the web view check to see if there are more then one of itself?


A webview instance can only be added to the view hierarchy once. If you have two webviews visible on screen at once, they are two different instances. You should keep track of these instances that you add to the hierarchy and when one dismisses, remove all the instances you are tracking from their superview.

You can also crawl a view hierarchy and look for instances of UIWebView.

for (UIView *subView in [myView subviews]) {
    if ([subView isKindOfClass:[UIWebView class]]) {
        [subView removeFromSuperview];
    }
}


mhm, no, wait, it's not so clear what you are meaning...

You say: "...there are more then one of itself" and "...gets allocated and displayed through multiple subviews"

You probably mean that you have 2 instances of "the same" UIWebView class, but then you should not consider them as "the same object" which lives in 2 different superviews... they are different objects, everyone has its own properties...

Or did i misunderstood?

So, if you meant as i said and you just want to control from a subView (mhm... or we should say from its UIViewController) if there are other views which use a UIWebView. I'd probably use one UIViewController "parent" where to load my subViews (eventually they could also have their own UIViewController, then every time i Alloc and addSubview a UIWebView in my subView i just add a tag to it:

myWebView.tag = 11;

it could change if needed for next one... the purpose is to be able to control if in my UIView there are allocated some UIWebView, now we can do that with this in my main parent UIViewController:

for (UIView *view_level_1 in [self.view subviews]) {

    for (UIView *view_level_2 in [view_level_1 subviews]) {

        if (view_level_1 >= 10) {

            // do something: dismiss this UIWebView too...

        }
    }
}

it just control in all the subViews of the view of my mainViewControl if there is a subView "tagged" before (a tag is a sort of "name/id")

I'm not sure the structure of your subView could be like that, it was not so clear in your question, but you can change the code adopting it...

luca


Use UIView's isDescendantOfView method to know if any subView is currently present on parentView.

if([addedSubView isDescendantOfView:parentView]) 
{   
   //addedSubView is subview of parentView
   //Take necessary action.
}
else
{
    //addedSubView is not subview of parentView
    //Take necessary action.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消