문제

First time working with a HUD and I'm confused.

I setup the HUD like this in my viewDidLoad:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    [[[WSXmppUserManager shared] xmppStreamManager] connect];

    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

The HUD doesn't show. I think the reason is as follows. The xmpp connect method fires off a connection request to the xmpp server and then it's done. So there is no activity to wait for as is.

However, the connection isn't established until the server responds and the following delegate method is fired:

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender

I want to wait for this and only then dismiss the HUD, but I'm at a loss as to how to do that. I'm missing something very simple.

도움이 되었습니까?

해결책

You need to move this code

dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});

After your long running method has finished... that is, if this code is indeed returning immediately

[[WSXmppUserManager shared] xmppStreamManager] connect];

The hud is likely never going to display... as it gets told to display and then told to hide on the same run loop or perhaps one run loop right afterwards...

Why not put it at the end of this method if this indicates that a response has been received and the operation is completed?

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender

다른 팁

HUD =[[MBProgressHUD alloc] initWithWindow:self.view];
[HUD setDelegate:self];
[self.view addSubview:HUD];

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

In the connectToServer

-(void)connectToServer
{
    [[[WSXmppUserManager shared] xmppStreamManager] connect];
}

As soon as the connectToServer method comepletes it task in the background , a delegate of MBProgressHUD called hudWasHidden: is automatically called

-(void)hudWasHidden:(MBProgressHUD *)hud
{
     //Further work after the background task completed
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top