开发者

Determine if an instance of a subview with specific name exists

What is the best way to开发者_开发百科 determine if an instance of a subview with a specific name exists? I have an application with a webview that adds a custom activity indicator in the form of a subview, using the webview delegate methods webviewDidStartLoad and webviewDidFinishLoad to add and remove the subview. I thought this was working fine, but I have since noticed that there are instances when webviewDidStartLoad and webviewDidFinishLoad do not necessarily fire in pairs. For example, if I start loading the webview and then send the app to the background (before the webview is finished loading) and then bring it back to the foreground I will end up with the activity indicator subview never getting removed (because when I send the app into the background webviewDidFinishLoad never gets called so when the app comes back to the foreground webviewDidStartLoad adds another instance of the view ontop of the old instance. Essentially that subview gets added twice and removed only once.

So what I want to do is only add the subview if it doesn't exist already and only remove it if it does in fact exist. So what is the best way to determine if the subview already exists or is showing? Thanks!


You should keep track of the subview in a property of the class in which you have the webview delegate methods. If the property is nil, there is no subview, and you should set it. When removing it, set the property to nil.

And/or instantiate it once, and add/remove the view without deallocing and allocing it over and over again.


Every view has a property "tag" which is an integer that you can use to identify view objects in you application. There is a method "viewWithTag" which returns the view whose tag matches the specified value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜