加载时显示子视图,完成时隐藏

发布于 2024-08-11 20:04:05 字数 1026 浏览 5 评论 0原文

我的设置:

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2024-08-18 20:04:05

如果您在同一事件回调中进行隐藏和显示所有操作,则您没有给它实际更新 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

为了更好的答案:
在执行加载的代码中,正在执行获取数据工作的代码执行以下操作:

// Code to show loading gui
[[[NSInvocationOperation alloc] initWithTarget:self 
               selector:@selector(refreshData) object:nil] autorelease];

然后对于刷新数据:

- (void) refreshData {
   // do the work
   [self performSelectorOnMainThread:@selector(doneRefresing) 
                          withObject:nil waitUntilDone:NO] ;
 }

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:

// Code to show loading gui
[[[NSInvocationOperation alloc] initWithTarget:self 
               selector:@selector(refreshData) object:nil] autorelease];

Then for refreshData:

- (void) refreshData {
   // do the work
   [self performSelectorOnMainThread:@selector(doneRefresing) 
                          withObject:nil waitUntilDone:NO] ;
 }
剩余の解释 2024-08-18 20:04:05

来自文档:

如果接收者的超级视图不是
nil,该方法释放
接收者。如果您打算重复使用
查看,之前一定要保留
调用此方法并确保
当你合适的时候释放它
完成后或将其添加到
另一个视图层次结构。

因此,通过使用“removeFromSuperview”,您实际上是在释放该对象并将其从其超级视图中删除。

From the docs:

If the receiver’s superview is not
nil, this method releases the
receiver. If you plan to reuse the
view, be sure to retain it before
calling this method and be sure to
release it as appropriate when you are
done with it or after adding it to
another view hierarchy.

Therefore by using 'removeFromSuperview' you are actually releasing the object as well as removing it from its superview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文