Can't display MBProgressHUD progress animation properly - ios

I would like to display a hud while the content is loading (and show the progress), but unfortunately it doesn't work properly. The hud appears on the screen when statusUpdate is 0.100000, but the loading bar doesn't moves until statusUpdate is not 1.000000 and the page loading finished. (After the view loaded sucessfully it animates from 0-100%.) I would appreciate if somebody could show me what I'm doing wrong.
// ViewDidLoad
[self.webView addObserver:self forKeyPath:#"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
HUD.delegate = self;
HUD.labelText = #"Uploading";
[HUD show:YES];
[self hud:self.webView.estimatedProgress];
if ([keyPath isEqualToString:#"estimatedProgress"] && object == self.webView) {
// [self.progressView setAlpha:1.0f];
// [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
NSLog(#"%f", self.webView.estimatedProgress);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
NSLog(#"%f", self.webView.estimatedProgress);
}
}
- (void) hud: (double)statusUpdate {
NSLog(#"STATUS %f", statusUpdate);
int myInt = (int)statusUpdate;
HUD.progress = (float)myInt;
}

Unless I'm missing something, the problem is on - (void) hud: (double)statusUpdate
For some reason you're casting the value (statusUpdate which is a double) to int and then again to a float, which means that 0.x values become 0.0 and 1.x values become 1.0 (that's why these are the only values that HUD is getting - since your range is 0.0/1.0)
A simple fix would be something like this:
- (void) hud: (double)statusUpdate {
NSLog(#"STATUS %f", statusUpdate);
HUD.progress = statusUpdate;
}

here is how i used it before:
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:vc.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.progress = 0;
hud.labelText = NSLocalizedString(#"Loading...", nil);
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
CGFloat progressValue = ((CGFloat)totalBytesRead/(CGFloat)totalBytesExpectedToRead);
MBProgressHUD *hud = [MBProgressHUD HUDForView:vc.view];
hud.progress = progressValue;
}];

Maybe you can create something like animation using CADisplayLink. It will update your HUD.
#interface ViewController () {
CADisplayLink *displayLink;
}
- (void)displayLinkMethod {
displayLink = [CADisplayLink displayLinkWithTarget:self selector:#selector(animationMethod)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

Related

iOS: How to check if a variable has changed his value in another ViewController in a dispatch_async?

I would like to know how to check if a value changed in another ViewController that is running in background. I have that code in that Viewcontroller that is downloading in background, and it puts me the isSyncAutoOnGoing to NO when the download is finished.
ViewController.m
#interface ViewController ()
{
BOOL isSyncAutoOnGoing;
}
- (void) sync:(id) sender
{
isSyncAutoOnGoing = YES;
dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NUL
L);
dispatch_async(downloadQueue, ^{
// do our long running process here
ListOfValueSync * lovSync = [[ListOfValueSync alloc] init];
// Synchronization
BOOL ret = [lovSync getAllListOfValueAll];
// do any UI stuff on the main UI thread
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinner removeFromSuperview];
isSyncAutoOnGoing = NO;
NSLog(isSyncAutoOnGoing ? #"sync Yes" : #"sync No");
if ([sender isKindOfClass:[UIButton class]])
{
self.loginField.text = #"";
(!ret) ? [self alertStatus:#"Can not synchronize" :#"Sync Failed!" :0] : [self alertStatus:#"Synchronization OK" :#"Sync OK" :0];
[sender setEnabled:TRUE];
}
});
});
}
}
Here is the FormViewController where I want to check when the isSyncAutoOnGoing is equal to NO. I know it is not the good way I think, and more, the value of isSyncAutoOnGoing is always equal to YES.
FormViewController.m
NSArray *viewControllers = [[self navigationController] viewControllers];
id obj;
for (int i = 0; i < [viewControllers count]; i ++)
{
obj = [viewControllers objectAtIndex:i];
if ([obj isKindOfClass:NSClassFromString(#"ViewController")])
{
BOOL isSyncAutoOnGoing = [[obj valueForKey:#"isSyncAutoOnGoing"] boolValue];
if (isSyncAutoOnGoing)
{
do{
[NSThread sleepForTimeInterval:1.0];
NSLog([[obj valueForKey:#"isSyncAutoOnGoing"] boolValue] ? #"isSyncAuto: YES" : #"isSyncAuto: NO");
}while([[obj valueForKey:#"isSyncAutoOnGoing"] boolValue]);
}
}
}
Any ideas?
Thanks in advance.
Suppose the array is in AppDelegate
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
self.allNewsItems = appDelegate.allNewsItems;
[appDelegate addObserver:self
forKeyPath:#"allNewsItems"
options:NSKeyValueObservingOptionNew
context:NULL];
and implement
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:#"allNewsItems"]){ if ([self isBeingPresented]){
[self.tableView reloadData]; }else{
self.mustReloadView = YES;
}
} }

Random EXC_BAD_ACCESS (code=1, address=0x4) when multithreading on iOS

I have a case, when I get EXC_BAD_ACCESS (code=1, address=0x4) exception randomly, when calling method with multi threads. Same method, sometime error accure, sometimes don't. But my measurements show that at least 1 out of 5 runs will produce this error.
Firstly let me show a diagram how my code is organized, so it will be easier to read the code.
I have one View controller that handle the view and call server class (instance stored in AppDelegate class) for data. Data there is retrieved from server asynchronously and store in Core data DB and call CompleteHandler when it is done.
In between, delegate method in server is called to update progress percentage in ViewControllers.
After this is done, ViewController call build Model class that is build asynchronously, and read data from Core data DB.
In this step, when model is called to build, error occurs.
Second problem is that even that delegate method is called and right percentage is send to ViewController, progress bar is not updated.
This, and fact that error come randomly, let me believe that it has to be something wrong with threading and that some object is released to soon.
I try debug with Zombies, but nothing shows up.
CODE :
ViewController
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// THIS IS NOTIFICATIONS FROM MODEL
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(modelProgressUpdate:) name:ProcessUpdateNotification object:nil];
self.coreDataQueue = dispatch_queue_create("com.my.core.data", NULL);
[self download1];
}
#pragma mark - Progres bar
// METHOD THAT IS CALLED AFTER NSNOTIFICATION IS CALLED
- (void) modelProgressUpdate:(NSNotification * ) notification{
BINotificationsProcessUpdate * update = (BINotificationsProcessUpdate * )notification.object;
[self updateProgressWithPercent:update.percent];
}
- (void) modelPrograssCancel
{
[self.progressViewContainer layoutSubviews];
[UIView animateWithDuration:0.5 animations:^{
self.ProgressViewTopConstraint.constant = -15;
[self.progressViewContainer layoutSubviews];
[self.view layoutSubviews];
}];
self.ProgressView.progress = 0;
}
// METHODS THAT ARE CALLED WITH SERVER DELEGATES
- (void) cancelProgresView
{
[self modelPrograssCancel];
}
- (void) progressInProcentage1:(float)procentage{
__weak ViewController * weekSelf = self;
dispatch_async(dispatch_get_main_queue(), ^(void) {
[weekSelf updateProgressWithPercent:procentage];
});
}
- (void) progressInProcentage2:(float)procentage{
__weak ViewController * weekSelf = self;
dispatch_async(dispatch_get_main_queue(), ^(void) {
[weekSelf updateProgressWithPercent:procentage];
});
}
- (void) updateProgressWithPercent: (float) percent
{
if (self.ProgressViewTopConstraint.constant != 0) {
[self.progressViewContainer layoutSubviews];
[UIView animateWithDuration:0.5 animations:^{
self.ProgressViewTopConstraint.constant = 0;
[self.progressViewContainer layoutSubviews];
[self.view layoutSubviews];
}];
}
self.ProgressView.progress = percent;
}
#pragma mark - download data
-(void)download1{
__weak ViewController * weekSelf = self;
// build new NSManagedObjectContext
weekSelf.backgroundManagedObjectContext = [[NSManagedObjectContext alloc]init];
[weekSelf.backgroundManagedObjectContext setPersistentStoreCoordinator:[[ManagedContextHelper getManagedObjectContext] persistentStoreCoordinator]];
[[NSNotificationCenter defaultCenter] addObserver:weekSelf selector:#selector(backgroundContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:weekSelf.backgroundManagedObjectContext];
AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.serverConnection callGet1:weekSelf.server managedObjectContext:weekSelf.backgroundManagedObjectContext delegate:weekSelf withCompletionHandler:^(BIResponseObject *response) {
switch (response.responseType) {
case BIResponseTypeNoInternet:
case BIResponseTypeConnectionTimeOut:
[weekSelf cancelProgresView];
break;
case BIResponseTypeOK:
[weekSelf download2];
break;
default:
[weekSelf loadModel];
[weekSelf noServerWarrning];
break;
}
}];
}
-(void)download2{
__weak ViewController * weekSelf = self;
AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.serverConnection callGet2:weekSelf.server managedObjectContext:weekSelf.backgroundManagedObjectContext delegate:weekSelf withCompletionHandler:^(BIResponseObject *response) {
switch (response.responseType) {
case BIResponseTypeFactTableOK: {
weekSelf.object.downlodedDate = [NSDate date];
[weekSelf.object save:weekSelf.backgroundManagedObjectContext];
[weekSelf loadModel];
[[ManagedContextHelper getManagedObjectContext] save:nil];
}
case BIResponseTypeConnectionTimeOut:
case BIResponseTypeNoInternet:
case BIResponseTypeFactTableError:
[weekSelf cancelProgresView];
break;
default:
[weekSelf cancelProgresView];
break;
}
}];
}
#pragma mark - LOAD MODEL
-(void)loadModel{
__weak ViewController * weekSelf = self;
dispatch_async(weekSelf.coreDataQueue, ^(void){
// build new NSManagedObjectContext
if (!weekSelf.backgroundManagedObjectContext) {
weekSelf.backgroundManagedObjectContext = [[NSManagedObjectContext alloc]init];
[weekSelf.backgroundManagedObjectContext setPersistentStoreCoordinator:[[ManagedContextHelper getManagedObjectContext] persistentStoreCoordinator]];
[[NSNotificationCenter defaultCenter] addObserver:weekSelf selector:#selector(backgroundContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:weekSelf.backgroundManagedObjectContext];
}
MyUIObject * uiObject = [DataBase getUIObject:weekSelf.backgroundManagedObjectContext];
weekSelf.myObject = [[UIScrollObject alloc] initWith: uiObject];
weekSelf.myObject.delegate = weekSelf;
if (uiObject && weekSelf.myObject) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
self.myObject.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.myObject];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_myObject]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_myObject)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(66)-[_myObject]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_myObject)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.myObject attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[self.view sendSubviewToBack:self.myObject];
// Add model
[self.myObject createModel]; // THIS IS A LINE THAT I THINK IT BREAK,... logs are till here.
});
}
else {
[weekSelf dissmisView:weekSelf];
}
});
}
- (void) dissmisView: (ViewController * ) selfReference {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[selfReference cancelProgresView];
if (![selfReference.presentedViewController isBeingDismissed]) {
[selfReference dismissViewControllerAnimated:YES completion:nil];
}
});
}
#pragma mark - handling multi ManagedObjectContext
- (void)backgroundContextDidSave:(NSNotification *)notification {
/* Make sure we're on the main thread when updating the main context */
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:#selector(backgroundContextDidSave:)
withObject:notification
waitUntilDone:NO];
return;
}
/* merge in the changes to the main context */
[[ManagedContextHelper getManagedObjectContext] mergeChangesFromContextDidSaveNotification:notification];
}
...
#end
MODEL created
dispatch_async(self.model_queue, ^(void){
// Create Model in new thread
[self createPrimaryModel_newThread]; // inside here notifications are send to update progress bar.
/// Model is complete
[BINotifications notifyModelIsCompletedCreating:self];
});
BINotifications
+ (void) notifyProgressUpdateWithPercent: (float) percent andNotificationString: (NSString * ) notificationString {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[[NSNotificationCenter defaultCenter] postNotificationName:ProcessUpdateNotification
object:[[BINotificationsProcessUpdate alloc]
initWithPercent:percent
description:notificationString]];
});
}
Server class
-(void)callGet2: (Server *) server managedObjectContext: (NSManagedObjectContext *)managedObjectContext delegate: (id) delegate withCompletionHandler:(void (^)(BIResponseObject * response))callback
{
id<BIServerConnectionDelegate> _delegate = delegate;
[_delegate progressInProcentage2:0.0];
NSString * method = [NSString stringWithFormat:#".../%i", ...];
[self callServerWithMethod:method server:server withCallBack:^(BIResponseObject *response) {
[_delegate progressInProcentage2:0.3];
// parse
response.responseType = [self parse2:response server:server managedObjectContext:managedObjectContext delegate:_delegate];
[self setResponseTitleAndMessage:response server:server];
callback(response);
}];
}
I know it is a lot, but I am really stuck with it.
Code is "cleaned", so name of functions,... are changed (I can't reveal real code.), so I know the names are not ok :D, but code is real.

MBProgressHUD gracetime and minimumtime not working properly. how to implement.?

I am using MBProgressHUD for showing loading while performing local long method to put user good experience. All working fine but now i have to implement like that if the method executes within a second than the loading should not be appear. I have gone through the MBProgressHUD samples and for this functionality i found the setGraceTime and setMinShowTime show time but its all not working properly. because when i put set grace time the loading icon is not appear even the method execution time is more than 1 second.Here is my code
if (self.HUD == nil)
{
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:self.HUD];
}
self.HUD.labelText = #"Please wait.....";
// [self.HUD setMinShowTime:2.0f];
[self.HUD setGraceTime:1.0f];
[self.HUD setTaskInProgress:YES];
[self.HUD show:YES];
// [self.HUD hide:YES afterDelay:3];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
if (mycondition == nil)
{
[self myTask:[NSURL fileURLWithPath:strPath]];
//[self performSelector:#selector(mytask:) withObject:[NSURL fileURLWithPath:strPath]];
}
else
{
//else
}
self.tempView.hidden = YES;
// self.HUD.taskInProgress = NO;
[self.HUD hide:YES];
self.HUD = nil;
I have putted [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; for this problem's solution
please guide me what wrong in this code..?!
You need to remove the MBProgressHUD object from its superview in the method execution.
Like this:
// if the method runs inside one second, the indicator will not be
shown
- (void)viewDidLoad
{
[super viewDidLoad];
if (HUD == nil)
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
}
HUD.labelText = #"Please wait.....";
[HUD setGraceTime:1.0f];
[HUD setTaskInProgress:YES];
[HUD show:YES];
[self performSelector:#selector(runThisMethod) withObject:nil afterDelay:0.9f];
}
- (void)runThisMethod
{
[HUD removeFromSuperview];
}
// if the method runs after one second, the indicator will be
shown for some time until the method runs
- (void)viewDidLoad
{
[super viewDidLoad];
if (HUD == nil)
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
}
HUD.labelText = #"Please wait.....";
[HUD setGraceTime:1.0f];
[HUD setTaskInProgress:YES];
[HUD show:YES];
[self performSelector:#selector(runThisMethod) withObject:nil afterDelay:1.9f];
}
- (void)runThisMethod
{
[HUD removeFromSuperview];
}

MBProgressHUD not resetting

I'm using MBProgressHUD in our teaching App, which is tab bar navigated.
The user will come from a tableview via Urban Airship's storefront directly to the UA detail view.
Once buy is clicked I bring on HUD with
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];
It is using the showWhileExecuting statement.
It goes through three while statements to change from "Connecting" to "Downloading" to "Unpacking".
All working perfectly OK.
Here comes the problem...
The second time I do this the label text will not change. It is stuck on "Connecting".
I can see in the NSLog that it is going through the other loops.
On top of that, if I try to change the Mode, the app crashes.
This only happens the second time, and any subsequent uses. If I kill the App everything works again for the first time.
Looks to me that MBProgressHUD doesn't get reset when it's finished.
(ARC is used in the project)
Anyone with a solution?
Thanks
Edit:
- (void)showWithLabelDeterminate
{
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
HUD.mode = MBProgressHUDModeIndeterminate;
[self.view.window addSubview:HUD];
HUD.delegate = self;
HUD.labelText = NSLocalizedString(#"Connecting","");
HUD.detailsLabelText = #" ";
HUD.minSize = CGSizeMake(145.f, 145.f);
HUD.dimBackground = YES;
[HUD showWhileExecuting:#selector(lessonDownloadProgress) onTarget:self withObject:nil animated:YES];
}
-(void)lessonDownloadProgress
{
DataManager *sharedManager = [DataManager sharedManager];
// HUD.mode = MBProgressHUDModeIndeterminate;
HUD.labelText = nil;
HUD.detailsLabelText = nil;
while ([sharedManager.downHUD floatValue] == 0.0f)
{
[self parentViewController];
NSLog(#"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.labelText = NSLocalizedString(#"Connecting","");
HUD.detailsLabelText = #" ";
NSLog(#"Waiting for download to start");
// Wait for download to start
usleep(80000);
}
// Switch to determinate mode
// HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = NSLocalizedString(#"DownLoading","");
HUD.progress = [sharedManager.downHUD floatValue];
while (HUD.progress < 1.0f && [sharedManager.cleanedUp isEqualToString:#"No"])
{
// [self parentViewController];
HUD.labelText = NSLocalizedString(#"Downloading","");
NSLog(#"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.progress = [sharedManager.downHUD floatValue];
NSString *percent = [NSString stringWithFormat:#"%.0f", HUD.progress/1*100];
HUD.detailsLabelText = [percent stringByAppendingString:#"%"];
usleep(50000);
}
// Switch HUD while cleanUp
HUD.mode = MBProgressHUDModeIndeterminate;
while ([sharedManager.cleanedUp isEqualToString:#"No"])
{
[self parentViewController];
HUD.labelText = NSLocalizedString(#"Unpacking","");
HUD.detailsLabelText = #" ";
// wait for cleanup
NSLog(#"Waiting for clean up");
usleep(50000);
}
NSLog(#"++ Finished loops ++");
NSLog(#"Finished HUD lessonDownloadProgress: %f", HUD.progress);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[HUD removeFromSuperview];
HUD.delegate = nil;
[HUD release];
HUD = nil;
}
I cannot spot the issue in the code you posted; but some refactoring might help.
Rather than polling the DataManager you could use KVO to observe properties on the DataManager and respond to those changes. ("Don't call us; we'll call you.) So here's a suggested approach if you want.
Your class interface:
#interface YourClass : UIViewController // or whatever your superclass is...
{
MBProgressHUD *_hud;
DataManager *_dataManager;
// your other ivars
}
#end
And in your implementation file...
#interface YourClass()
#property (nonatomic, retain) DataManager dataManager;
#end
Above I've declared your dataManager as a property so that we can observe it.
To start the download process we now have a method downloadLesson:
- (void)downloadLesson;
{
// show HUD and retain it (showHUDAddedTo:animated: returns autoreleased object)
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
// observe properties on the dataManager
[self addObserver:self forKeyPath:#"dataManager.progress" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:#"dataManager.cleanedUp" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:#"dataManager.downHUD" options:NSKeyValueObservingOptionNew context:nil];
// begin your download here...
HUD.labelText = NSLocalizedString(#"Connecting", "");
HUD.detailsLabelText = #" ";
HUD.progress = self.dataManager.downHUD;
}
Now use KVO to update the appearance of the HUD:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
if( [keyPath isEqualToString:#"dataManager.cleanedUp"] )
{
if( [[[self dataManager] cleanedUp] isEqualToString:#"Yes"] )
{
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
[HUD release]; HUD = nil;
[self removeObserver:self forKeyPath:#"dataManager.progress"];
[self removeObserver:self forKeyPath:#"dataManager.cleanedUp"];
[self removeObserver:self forKeyPath:#"dataManager.downHUD"];
}
}
if( [keyPath isEqualToString:#"dataManager.downHUD"] )
{
// if the data manager updates progress, update our HUD
HUD.progress = self.dataManager.downHUD;
if( self.dataManager.downHUD == 0.0 )
// no progress; we're just connecting
HUD.labelText = NSLocalizedString(#"Connecting", "");
else if( self.dataManager.downHUD < 1.0 )
{
// progress >0.0 and < 1.0; we're downloading
HUD.labelText = NSLocalizedString(#"Downloading", "");
NSString *percent = [NSString stringWithFormat:#"%.0f%%", HUD.progress/1*100];
HUD.detailsLabelText = percent;
}
else
{
// progress == 1.0, but we haven't cleaned up, so unpacking
if( [[[self dataManager] cleanedUp] isEqualToString:#"No"] )
{
HUD.labelText = NSLocalizedString(#"Unpacking","");
HUD.detailLabelsText = #" ";
}
}
}
}
Alternatively, you could use notifications to do the updates, wherein the DataManager posts NSNotifications for which your view controller is registered. Or, if you were open to refactoring the DataManager you could use blocks to do the updates. All of these solutions avoid having to explicitly block your thread to poll the DataManager. Hope this helps.
Did you implement the delegate method?
- (void)hudWasHidden:(MBProgressHUD *)hud {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
HUD = nil;
}
This is because the label is set up on init.Try this:
You should just add this method to the header file of MBProgressHud:
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text;
And implement it in the .m file as follows:
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text
{
MBProgressHUD *hud = [[self alloc] initWithView:view];
hud.labelText = text;
[view addSubview:hud];
[hud show:YES];
return MB_AUTORELEASE(hud);
}
and call it wherever you want like:
[MBProgressHUD showHUDAddedTo:self.view withText:#"Loading..."];

MBProgressHUD indicator does not hide

i am able to show HUD indicator in viewDidLoad successfully but not able hide it in webViewDidFinishLoad method when webview is completely loaded. Please help.
i am using below code::
in .h file
MBProgressHUD *HUD;
in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *query = [[NSString alloc] initWithFormat:#"http://localhost/index.php?uid=%#", [[UIDevice currentDevice] uniqueIdentifier]];
NSURL *url = [[NSURL alloc] initWithString:query];
NSString *response = [[NSString alloc] initWithContentsOfURL:url];
if(response)
{
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
else
{
//NSLog(#"err %#",response);
}
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
HUD.delegate = self;
HUD.labelText = #"loading";
}
and in webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[HUD hide:TRUE]; //it does not work for me :(
}
i have fixed the error, i moved the code from viewDidLoad to webViewDidStartLoad and everything is working fine this time :)
- (void)webViewDidStartLoad:(UIWebView *)web
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = #"loading";
}
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
try with this one
[HUD hide:YES];
if(HUD!=nil && [HUD retainCount]>0)
{
[HUD removeFromSuperview];
[HUD release];
HUD=nil;
}
You should not call MBProgressHUD from viewDidLoad, try calling it from viewDidAppear and everything should work well.
Try removing it using this class method:
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated
.
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
If you use this method then you should think about rewriting your viewDidLoad this way:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = #"loading";
}

Resources