MBProgresshud with tableview

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:22:49

问题


I am making a application with a tableview in it. I would like to implement a loading screen, using MBProgressHUD such that it will display before data is read from internet. However the data's not shown using following code:

- (void)viewDidLoad
{

HUD = [[MBProgressHUDalloc] initWithView:self.view];

[self.viewaddSubview:HUD];
HUD.delegate = self;



[HUD showWhileExecuting:@selector(load_data) onTarget:self withObject:nil animated:YES];


}

the data can be shown in tableview using the function load_data alone (i.e [self load_data], but not with HUD.


回答1:


In my experience, when using the HUD to display while loading or waiting for data to load, you should call the HUD in the -viewDidAppear method. I also noticed that you didn't include the [super viewDidLoad]; call in your code. If you are going to present your HUD, you will have to call it after you call on the super viewDidLoad if you want it to appear. Hopefully these help you out.




回答2:


I like to present and hide the HUD with separate methods that only do that. e.g.

#pragma mark - The HUD

-(void)showHudWithText:(NSString *)text {   
   if (self.hud == nil) {
      self.hud = [[[MBProgressHUD alloc] initWithWindow:self.window] autorelease];
      [self.window addSubview:hud];
   }

   [self.hud setLabelText:text];
   [self.hud setMode:MBProgressHUDModeIndeterminate];
   [self.hud show:YES];
}

-(void)hideHud {
   [self.hud hide:YES];
}

This allows the HUD to be controlled independently of the view life cycle, as well as from asynchronous methods, timers, etc. e.g:

-(void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHudWithText:) name:kSomethingImportantStartedNotification object:@"Starting..."];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideHud) name:kSomethingImportantEndedNotification object:nil];
}

Or something like that.



来源:https://stackoverflow.com/questions/7006409/mbprogresshud-with-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!