MBProgresshud with tableview

2019-08-17 16:16发布

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.

2条回答
看我几分像从前
2楼-- · 2019-08-17 16:34

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.

查看更多
混吃等死
3楼-- · 2019-08-17 16:36

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.

查看更多
登录 后发表回答