加载时显示子视图,完成时隐藏
我的设置:
在我的iPhone应用程序中,我有一个加载视图(它本身就是一个UIImageView)和加载视图上的两个子视图,一个UIIndicatorView和一个UILabel。要查看它,我调用 [self.view addSubview:loadingView]
方法,要隐藏它,我使用 [loadingView removeFromSuperView]
。
在我的应用程序中刷新数据我有方法-(void)refreshData
。在此方法中,我显示子视图,下载 HTTP POST 生成的一些数据,并使用该数据启动 NSXMLParser。
当 NSXMLParser 到达其 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName
方法的最后一个元素时,然后它隐藏子视图(使用上面的方法)。
要调用 refreshData
方法,我的导航栏中有一个刷新按钮:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshData)] ;
我的问题:
当我单击刷新按钮时,子视图不显示。它调用这些方法是因为在日志中我清楚地看到:
显示加载视图
在加载过程中显示的一堆行(总共 15 秒)
隐藏加载视图
我也知道我的方法有效,因为当我注释掉隐藏视图的方法时在我关闭应用程序之前,将继续显示正在加载视图
有什么想法吗?
My Setup:
In my iPhone app, I have a loading View (which is an UIImageView itself) and two subviews on the loading View, an UIIndicatorView and an UILabel. To view it, I call the [self.view addSubview:loadingView]
method, and to hide it i use [loadingView removeFromSuperView]
.
In my app to refresh my data i have the method -(void)refreshData
. in this method, I show the Subview, download some data resulting from an HTTP POST, and start and NSXMLParser with this data.
when the NSXMLParser reaches the last element of it's -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
method, it then hide the subview (using the above method).
To call the refreshData
method i have a refresh button in my Navigation Bar:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshData)];
My Issue:
When I click the refresh button the subview does not show. It calls the methods because in the Log I clearly see:
Showing Loading View
Bunch of lines shown during the loading process (totalling 15 seconds)
Hiding Loading View
I also know that my methods work because when I comment out the one that hides the view the Loading View continues to be seen until I close the app
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在同一事件回调中进行隐藏和显示所有操作,则您没有给它实际更新 GUI 的机会。如果你的工作需要花费很多时间(比如 15 秒),你可以使用 NSOperation 来执行操作,并在完成回调发生时隐藏你的视图。
http://developer.apple.com/ iphone/library/documentation/cocoa/Reference/NSOperation_class/Reference/Reference.html
这可能更容易使用:
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSInitationOperation_Class/Reference/Reference.html#//apple_ref/occ/cl/NSInitationOperation
当你想更新 gui 时,确保您是从主线程执行的,而不是从 NSOperation 方法中的事件执行的:
您可以使用此方法在主线程中运行选择器。
http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/ NSObject/performSelectorOnMainThread:withObject:waitUntilDone:
为了更好的答案:
在执行加载的代码中,正在执行获取数据工作的代码执行以下操作:
然后对于刷新数据:
If you are doing the hiding and showing all within the same event callback, you haven't gave it a chance to actually update the gui. If your work is going to take alot of time(like the 15 seconds), you could use NSOperation to do the operation and have when the finish callback occurs then hide your view.
http://developer.apple.com/iphone/library/documentation/cocoa/Reference/NSOperation_class/Reference/Reference.html
this might be easier for you to use:
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/NSInvocationOperation_Class/Reference/Reference.html#//apple_ref/occ/cl/NSInvocationOperation
When you want to update the gui, make sure you do from the main thread not from the event in the NSOperation method:
You can use this method to run the selector in the main thread.
http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:
For a better answer:
In the code doing the loading the one that was doing the work to get the data do:
Then for refreshData:
来自文档:
因此,通过使用“removeFromSuperview”,您实际上是在释放该对象并将其从其超级视图中删除。
From the docs:
Therefore by using 'removeFromSuperview' you are actually releasing the object as well as removing it from its superview.