Reputation: 4223
I cannot display Indicator View.
ItemController.h
#import <UIKit/UIKit.h>
@interface ItemController : UITableViewController {
UIView* loadingView;
UIActivityIndicatorView* indicator;
}
@property (nonatomic, retain) UIView *loadingView;
@property (nonatomic, retain) UIActivityIndicatorView *indicator;
@end
ItemController.m
.....
- (void)netAccessStart {
loadingView = [[UIView alloc] initWithFrame:[[self view] bounds]];
[loadingView setBackgroundColor:[UIColor blackColor]];
[loadingView setAlpha:0.5];
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[[self view] addSubview:loadingView];
[loadingView addSubview:indicator];
[indicator setFrame:CGRectMake ((320/2)-20, (480/2)-60, 40, 40)];
[indicator startAnimating];
}
- (void)netAccessEnd {
[indicator stopAnimating];
[loadingView removeFromSuperview];
}
- (void)dealloc {
[loadingView release];
[indicator release];
[super dealloc];
}
.....
inherited class
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self netAccessStart];
sleep(1);
[self netAccessEnd];
}
Upvotes: 0
Views: 312
Reputation: 96323
It's because you're using sleep
instead of actually doing something. Presumably, you mean to retrieve something from the internet; do that, and send yourself that netAccessEnd
message only when the connection either finishes or fails.
Replacing sleep
with another form of delay that still uses a hard-coded interval (e.g., 1 second) is a bad idea. If it takes less than 1 second, you're making the user wait longer than necessary. If it takes longer than 1 second, you're taking the indicator down before you are actually done with whatever the user is waiting for.
You need to take the indicator down when whatever the user is waiting for finishes—no sooner, no later.
Upvotes: 0
Reputation: 57149
sleep()
blocks execution, meaning that your app is freezing on the -viewWillAppear
call, at the end of which your loadingView
gets removed from its superview. In other words, no drawing occurs between [self netAccessStart];
and [self netAccessEnd];
. I assume you're calling one immediately after the other for testing purposes—in that case, I'd move the -netAccessStart
command to -viewDidAppear:
and replace the sleep
/-netAccessEnd
call with the following:
[self performSelector:@selector(netAccessEnd) withObject:nil afterDelay:1];
...which will have the same effect but without blocking execution.
Upvotes: 1