确定是否存在具有特定名称的子视图实例
确定是否存在具有特定名称的子视图实例的最佳方法是什么?我有一个带有 webview 的应用程序,它以子视图的形式添加自定义活动指示器,使用 webview 委托方法 webviewDidStartLoad 和 webviewDidFinishLoad 来添加和删除子视图。我认为这工作正常,但我后来注意到,有些情况下 webviewDidStartLoad 和 webviewDidFinishLoad 不一定成对触发。例如,如果我开始加载 webview,然后将应用程序发送到后台(在 webview 完成加载之前),然后将其带回前台,我最终将得到活动指示器子视图永远不会被删除的结果(因为当我发送进入后台的应用程序 webviewDidFinishLoad 永远不会被调用,因此当应用程序返回到前台时,webviewDidStartLoad 在旧实例之上添加了另一个视图实例,本质上该子视图被添加了两次,并且只被删除了一次
。如果子视图尚不存在,则添加它;如果确实存在,则仅删除它 那么确定子视图是否已存在或正在显示的最佳方法是什么?
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在具有 webview 委托方法的类的属性中跟踪子视图。如果该属性为nil,则没有子视图,您应该设置它。删除它时,将该属性设置为零。
和/或实例化一次,然后添加/删除视图,而无需一次又一次地取消分配和分配它。
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.
每个视图都有一个属性“标签”,它是一个整数,可用于标识应用程序中的视图对象。有一个方法“viewWithTag”,它返回标签与指定值匹配的视图。
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.