开发者

uiactivityindicator when page is loading

How can I show an uiactivityindicator when I开发者_运维知识库 click a cell in my tableview as sometimes it takes a few moments for it to get pushed to the next view since it's getting information from the internet.

I have successfully done it for my uiwebview but just stuck on this.

Thanks.


-(void)viewDidLoad { webPage.self = delegate; indicator.hidesWhenStopped = TRUE; }

–(void)webViewDidStartLoad:(UIWebView *)webView { [indicator startAnimating]; }

–(void)webViewDidFinishLoad:(UIWebView *)webView { [indicator stopAnimating]; }


-(void)viewDidAppear:(BOOL)animated {
[super viewdDidAppear:YES];
[indicator startAnimating];
NSTimer* timerLoad = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(stopIndicator) userInfo:nil repeats:NO];
}

-(void)stopIndicator {
[indicator stopAnimating];
}

I think this give you a time for load your tableView

Put it in navigation controller

indicator.frame = CGRect(11,130,37,37);
[navBar addSubview:indicator];


You should rather perform the fetching data in the next view controller on a separate thread (in background) so that it doesn't blocks the UI. And Until the data fetching is done, show activity indicator there.


(void)viewDidLoad {

//We have a NIB file in play here, so I dropped the loadView here. Just make sure that your loadView is not getting called twice!

[super viewDidLoad];
[self loadView];

}

  • (void)loadView {

    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; self.view = contentView;

    CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; webFrame.origin.y = 0.0f; myWebView = [[UIWebView alloc] initWithFrame:webFrame]; myWebView.backgroundColor = [UIColor blueColor]; myWebView.scalesPageToFit = YES; myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); myWebView.delegate = self; [self.view addSubview: myWebView];

    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.xyz.com/"]]];

    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); activityIndicator.center = self.view.center; [self.view addSubview: activityIndicator]; }

  • (void)dealloc { [activityIndicator release]; [myWebView release]; [super dealloc]; }

pragma mark WEBVIEW Methods

  • (void)webViewDidStartLoad:(UIWebView *)webView { // starting the load, show the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [activityIndicator startAnimating]; }

  • (void)webViewDidFinishLoad:(UIWebView *)webView { // finished loading, hide the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [activityIndicator stopAnimating]; }

  • (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { // load error, hide the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // report the error inside the webview

    NSString* errorString = [NSString stringWithFormat: @"

    Error

    Your request %@", error.localizedDescription]; [myWebView loadHTMLString:errorString baseURL:nil];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜