开发者

iPhone loading view from nib only works once

I'm trying to add a «loader-view» to my app which shows a spinner while doing stuff.

This works fine the first time, but it doesn't work a second time.

here's what I do:

I have a viewController for the spinner (spinnerViewController) and a nib-file which I made in IB (spinner.xib).

I load the nib in the viewDidLoad-event:

spinnerView = [[spinnerViewController alloc] initWithNibName:@"spinner" bundle:nil];

[spinnerView retain];

spinnerView is declared in the .h-file (spinnerViewController *spinnerView;)

next, I show the spinner-view:

[self.view addSubview:spinnerView.view];
 [self.view bringSubviewToFront:spinnerView.view];

which works fine...

And now the trouble starts. No matter what I do, I can't show the spinner view again.

I tried just hiding it (self.view sendSubViewToBack: spinnerView.view) which works for hiding, but when I try to bring it to the front again (self.view bringSubViewToFront: spinnerView.view) it doesn't work.

开发者_如何学运维

I also tried removing the spinner-view and add it again with no success (within the spinnerViewController: [self.view removeFromSuperview] and to show it again [self.view addSubview... )

[EDIT]

I changed the whole setup a little and just made the spinner-view a subview in IB - again, hiding works, but showing again fails. What I found out: After the bringSubViewToFront-command, I call some web-service to get some data. When I comment the following code out and just show the spinnerView, it works. So now I'm trying to figure out how to determine when the spinner-view appeared and then continue with the code - but naturally, this doesn't work (yet) :)

Any ideas what I'm doing wrong??? ;)


Problem solved.

This page gave the answer: http://urenjoy.blogspot.com/2009/05/uiactivityindicatorview.html

Apparently, the update has to happen in a separate thread, as the web-stuff blocks the current one, hence the view did not appear.

[NSThread detachNewThreadSelector:@selector(doWork) toTarget:self withObject:nil];
- (void) doWork {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

.....Time Consuming Code here .....

[pool release];

} 


I might be not exactly on your question, but in general creating a ViewController class in order to show a spinner on the screen is a huge overkill ... just try to discover the logic behind what you do : you create a viewcontroller, but you never use it, you use the view.

So in short I believe you need only a UIView (the view property of the UIViewController) Why don't you try something like :

... in your class interface... 

UIActivityIndicator* activity;

... when the activity needs to happen ...

activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle: .... 
[activity startAnimating];
[self.view addSubview:activity];
[activity release]

... when the activity is finished

[activity removeFromSuperview]; //this will also decrease the retain count
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜