Sometimes MBProgressHUD is not displaying - iphone

I have list of song titles displayed in uitableview along with "Buy" button.when that button is tapped i am showing MBProgressHUD.But sometimes it is not being displayed.also it disables the user-interaction as it is in the below code.
But why it is not displaying the MBProgressHUD sometimes?
Please let me know, Thanks a lot.
Below is the code
-(void) buySong:(UIButton *)button
{
self.view.userInteractionEnabled = NO;
self.navigationController.navigationBar.userInteractionEnabled = NO;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Proessing...";
hud.yOffset = -80;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [[self tblViewChildrenPoems] indexPathForCell:cell];
PSSongTags *songTags = [self.songsArray objectAtIndex:indexPath.row];
[ [PurchaseViewController sharedPurchaseManager] startPurchase:songTags];
}

Try this code may be helped you...
In your .h file import the .
#import "MBProgressHUD.h"
the set the delegate .
MBProgressHUDDelegate
After
MBProgressHUD *HUD;
in your .m file // Add this code where you want display ...
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Authorizing...";
[HUD show:YES];
and when your process end use for hide ..
[HUD Hide:YES];
and set hide delegate in your m file also..
- (void)hudWasHidden:(MBProgressHUD *)hud {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}
Happy coding...

Related

UIActivityIndicatorView doesn't render

I have reviewed many posts regarding this issue with no success. I have two table view controllers, a source and a destination, both in a navigation controller. When a user taps a cell in the source table view controller, an object is instantiated which makes a web service call that could take several seconds depending on network speed. Once this web service call is made, the segue is executed and navigation moves from the source to the destination. I want an activity indicator to show when this web service call is made. Nothing I have tried for the past several days has worked, even posts marked as successful on this forum. So, there must be something... Here is my code thus far:
Source table view controller header:
#property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
Source table view controller implementation:
#synthesize activityIndicator;
-(void)loadView {
[super loadView];
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:self.activityIndicator];
self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *stateAbbr;
if([segue.identifier isEqualToString:#"sgShowStateDetail"]){
DetailTableViewController *detailVC = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:path.section];
NSString *key = [tempArray objectAtIndex:path.row];
stateAbbr = [statesDict objectForKey:key];
[detailVC setStateIdentifier:stateAbbr];
// begin activity indicator here
[self.view bringSubviewToFront:activityIndicator];
[activityIndicator startAnimating];
gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
[activityIndicator stopAnimating];
[detailVC setStateGauges:gauges];
// end activity indicator here
}
}
GaugeList is the object that performs the web service call.
Everything works as expected, except there is never an activity indicator. There are no errors. What am I doing wrong? Thanks!
I think you might be using the wrong pattern. The call to the web service has to happen in the background, while the activity indicator has to be shown and hidden on the main thread. Thus
__block NSArray *gauges;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator stopAnimating];
[detailVC setStateGauges:gauges];
};
};
Here is the code using the method tableView: didSelectRowAtIndexPath: rather than prepareForSegue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view bringSubviewToFront:activityIndicator];
[activityIndicator startAnimating];
dispatch_async(dispatch_queue_create("", nil), ^ {
NSString *stateAbbr;
DetailTableViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailTableViewController"];
NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:indexPath.section];
NSString *key = [tempArray objectAtIndex:indexPath.row];
stateAbbr = [statesDict objectForKey:key];
[detailVC setStateIdentifier:stateAbbr];
gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
[detailVC setStateGauges:gauges];
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator stopAnimating];
[self.navigationController pushViewController:detailVC animated:YES];
});
});
}

How use two subsequent MBProgressHUD messages

I wan to to show a message using the MBProgressHUD during the uploading via NSURL string. So i code that:
MBProgressHUD *hud1 = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud1.labelText = #"sending info ...";
hud1.minShowTime =10.0;
NSURL *url =[NSURL URLWithString:self.urlToUpload];
the 10 second time is in order to wait some time i want user wait. It works, but I would show another message when the first desappear. Using another:
MBProgressHUD *hud2 = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud2.labelText = #"SENT!";
hud2.minShowTime =2.0;
it is not useful, because this message overlaps the first one.
Any tips about that?
I would remove the first HUD in your -connectionDidFinishLoading method or wherever you get notification that the string has been uploaded successfully.
[MBProgressHUD hideHUDForView:self.view animated:YES];
Then you can add your next HUD for 1-2 seconds. By just adding it via a specific time, there is no way to accurately remove the first HUD before the next one shows.
This was taken from the MBProgressHUD demo project. I would use this to display your timed completion HUD.
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
// The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"37x-Checkmark.png"]] autorelease];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = self;
HUD.labelText = #"Completed";
[HUD show:YES];
[HUD hide:YES afterDelay:3];
+(void)toast:(NSString*) str view:(UIView*)view delegate:(id)delegate type:(IconType)type{
[MBProgressHUD hideHUDForView:view animated:YES];
MBProgressHUD * HUD = [[MBProgressHUD alloc] initWithView:view];
[view addSubview:HUD];
NSString* strImage;
switch (type) {
case kNone:
strImage=nil;
break;
case kOK:
strImage=#"37x-Checkmark.png";//NSString stringWithFormat:#"%#",
break;
case kError:
strImage=#"ic_cancel_white_24dp.png";//NSString stringWithFormat:#"%#",
break;
default:
break;
}
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:strImage] ];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = delegate;
HUD.labelText = str;
HUD.userInteractionEnabled = NO;
[HUD show:YES];
[HUD hide:YES afterDelay:1.5f];
}

performSelector: withObject: afterDelay not executing for MBProgressHUD

I have the following method for a MBProgressHUD:
[progressHUD performSelector:#selector(hide:)
withObject:[NSNumber numberWithBool:YES]
afterDelay:kMessageHidingDelay];
the delay is 2.0 here, however it's not calling hide after 2.0 seconds. I tried to put a breakpoint in the hide function and it's not getting there. Any idea? Here's the full code:
progressHUD = [[MBProgressHUD alloc] initWithView:viewToAttach];
// Add HUD to screen
[viewToAttach addSubview:progressHUD];
progressHUD.labelText = #"Logging In";
progressHUD.removeFromSuperViewOnHide = YES;
// Show the HUD while the provided method executes in a new thread
[progressHUD show:YES];
May be try to perform selector on Main thread (all UI changes must be done on main thread)?
performSelectorOnMainThread:
you have to hide the MBProgressHud
[progressHUD hide:YES];
To Show MBProgressHUD Use this Code:-
HUD = [[MBProgressHUD alloc] init];
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
where myTask is
- (void)myTask
{
"Your Code"
}
And too Hide the MBProgressHud
- (void)hudWasHidden:(MBProgressHUD *)hud
{
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}
And If you Want to Show Hud With Your CostomView Then Use This Code
HUD = [[MBProgressHUD alloc] init];
[self.view addSubview:HUD];
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image Name.png"]] autorelease];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = self;
HUD.labelText = #"Completed";
[HUD show:YES];
[HUD hide:YES afterDelay:3];
}

MBProgressHUD call from ViewDidLoad

I call my rss parser from MBProgressHUD and at the moment it's in viewdidappear and it works, however I want it in viewdidload so when I go back it doesn't reload it - however when I move it in to viewdidload it runs the process but not the MBProgressHUD.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Results";
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"updating data";
//Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:#selector(loadResults) onTarget:self withObject:nil animated:YES];
}
Any ideas?
Thanks, Tom
In viewDidLoad method, the self.view will be loaded without its superview and window. For Example, self.view.superview will be loaded in viewWillAppear method, then self.view.window will be loaded in viewDidAppear method. That's why the [self.view.window addSubview:HUD] won't be happened in viewDidLoad, but the [self.view addSubview:HUD] works.
Try below code, its working fine at my end.
- (void)viewDidLoad {
[super viewDidLoad];
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"updating data";
}

UITableView addSubview and table lines

I'm trying to add a custom view to an UITableView created with code from an UITableViewController. This view is a HUD window with a message (MBProgressHUD)
So I have a method reload() called from the overridden initWithStyle() method and from the refresh button of the table:
- (void) reload {
HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Downloading";
[HUD showWhileExecuting:#selector(reloadWithHUD) onTarget:self withObject:nil animated:YES];
}
The first time, the HUD appears behind the table lines. Once loaded, when I press reload button, the view shows as expected. The initWithStyle() method (with some code removed for clarity) is:
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
self.title = NSLocalizedString...
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle ... action:#selector(reload)];
self.navigationItem.rightBarButtonItem = button;
[button autorelease];
[self reload];
}
return self;
}
I've tried changing the HUD view with a simple UILabel, with the same result.
I also changed the code to call reload() from viewDidLoad(), but doesn't work either. How can I resolve this issue? Thank you very much.
EDIT : to clarify this, here is a possible solution to this problem. Many thanks to Bill Brasky for his help:
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[[[UIApplication sharedApplication] keyWindow] addSubview:HUD];
Here is what I'm doing in my app with an almost identical application.
You need to add it to the main view WINDOW, not the tableView.
HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
[self.view.window addSubView:HUD];
My project is based on IOS 7
This helps me
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];