UIActivityIndicatorView not working right - ios

I have the following initialization in viewDidLoad:
self.indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.indicator setCenter:CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/2)];
//self.indicator.hidden = YES;
[self.indicator setHidesWhenStopped:1];
self.indicator.color = [UIColor orangeColor];
self.indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
Now if I add the following 2 lines, the indicator shows and spins:
[self.view addSubview:self.indicator];
//self.indicator.hidden = NO;
[self.indicator startAnimating];
Thats not what I want, because the indicator should first show when I call my method, lets call it pressButton. I would then do the following:
- (void)pressButton {
[self.view addSubview:self.indicator];
[self.indicator startAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
WebAccess *web = [WebAccess new];
NSDictionary *dicResults = [web logon:self.email.text :self.password.text];
if([[dicResults valueForKey:#"StatusCode"] intValue] == 200){// ALL IS OK
appDelegate.accessToken = [dicResults valueForKey:#"AccessToken"];
dicResults = [web getAccount:appDelegate.accessToken];
NSLog(#"dicResults after getaccount: %#", dicResults);
if([[dicResults valueForKey:#"StatusCode"] intValue] == 200){//dicResults holds a new object now!
NSArray *usersArray = [dicResults valueForKeyPath:#"Account.Users"];
NSLog(#"usersArray: %#", usersArray);//CORRECT DATA RECEIVED: YES
if(usersArray.count){
[[appDelegate cdh]deleteAllFromEntityWithName:#"AccountData"];
[[appDelegate cdh]deleteAllFromEntityWithName:#"UserData"];//------------- REMOVE ALL WHEN LOADING NEW USER. AMYBE TOO QF. CHECK WITH DESIGNER
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"No users found on this account!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
}
//more code........
appDelegate.users = [NSMutableArray arrayWithArray:[[appDelegate cdh] fetchUsers]];
if(appDelegate.users.count){
NSArray *currentUserArray = [NSArray arrayWithArray:[[appDelegate cdh]fetchCurrentUser]];
appDelegate.currentUser = [currentUserArray lastObject];
if(appDelegate.currentUser==nil)
appDelegate.currentUser = appDelegate.users[0];
}
[menu goToVC:1];
}
}else if([[dicResults valueForKey:#"StatusCode"] intValue] == 201){
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Wrong email or password!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
}
[self.indicator stopAnimating];
});
}
I could've sworn I had it working yesterday when testing, but now it doesn't. Whats weird is if i put those same two lines from my press button method into viewdidload, the indicator shows, but when I do it inside my method (but obv outside the dispatch) it never shows....
Any help appreciated

Spin your long running process off onto its own thread. Leave the main thread open for UI updates.
- (void)pressButton
{
[self.view addSubview:self.indicator];
[self.indicator startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Code for your long running process to run in the background...
// Stop your activity indicator back on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.indicator stopAnimating];
});
});
}

Related

How to redirect to home page after SCLAlertView showWaiting alert?

I would like to show a SCLAlertView alert and after few seconds, it will disappear and redirect to other viewcontroller. But now what i did is after click on "Done" button, the action only can be executed. Any idea? Please help.
Here is my code:-
-(IBAction)submitButtonDidPressed:(id)sender {
NSString *userName = [NSString stringWithFormat:#"Welcome back %#", txtUsername.text];
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert addButton:#"Done" target:self selector:#selector(clickLogin:)];
alert.customViewColor = ThemeBlueColor;
[alert showWaiting:self title:#"Welcome" subTitle:userName closeButtonTitle:nil duration:2.0f ];
}
-(void) clickLogin:(UIButton*)sender
{
int index = 0;
self.tabBarController.selectedIndex = index;
[self.tabBarController.viewControllers[index] popToRootViewControllerAnimated:NO];
}
You can use alertIsDismissed to call navigation like this:
-(IBAction)submitButtonDidPressed:(id)sender {
NSString *userName = [NSString stringWithFormat:#"Welcome back %#", txtUsername.text];
SCLAlertView *alert = [[SCLAlertView alloc] init];
[alert addButton:#"Done" target:self selector:#selector(clickLogin:)];
alert.customViewColor = ThemeBlueColor;
[alert showWaiting:self title:#"Welcome" subTitle:userName
closeButtonTitle:nil duration:3.0f ];
[alert alertIsDismissed:^{
[self clickLogin:nil];
}];
}

data is not loading in tableView and not updating profile

I am new in objective C.Execute this Program ON Clicking on button"Click here to sell your Gold now"
It will lunch you to dashboard.In dashboard select Transaction History.I have fetchNewHothistory function in TransactionHistoryViewController.m file responsible for fetching data from url and displaying it onto the viewcontroller file
-(void)fetchNewHothistory
{
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:1.0
target: self
selector:#selector(mytimerChecking:)
userInfo: nil repeats:NO];
NSMutableDictionary *paramDict=[NSMutableDictionary dictionary];
[paramDict setObject:#"ios" forKey:#"request"];
[paramDict setObject:[NSString stringWithFormat:#"%#",currentUser.user_id] forKey:#"user_id"];
[paramDict setObject:[NSString stringWithFormat:#"%#",self.currentLimitNew] forKey:#"limit_start"];
[GeneralWebservices webserviceMainSplashCall:paramDict webserviceName:Webservice_TransactionHistory OnCompletion:^(id returnDict, NSError *error) {
if ([returnDict[#"success"] intValue] ==1)
{
// UIAlertView* alert = [[UIAlertView alloc] init];
// [alert setTitle:#"RECORD FOUND"];
// // [alert setMessage:returnDict[#"message"]];
// [alert addButtonWithTitle:#"OK"];
// [alert show];
[history addObjectsFromArray:returnDict[#"data"]];
self.currentLimitNew=[NSString stringWithFormat:#"%#",returnDict[#"limit_start"]] ;
[historyTableView reloadData];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] init];
[alert setTitle:#"RECORD FOUND"];
//[alert setMessage:returnDict[#"message"]];
[alert addButtonWithTitle:#"OK"];
[alert show];
}
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[historyTableView.pullToRefreshView stopAnimating];
[historyTableView.infiniteScrollingView stopAnimating];
}];
}
Issue -1
Record found but nothing displayed on Viewcontroller on selecting the Transaction History.The How to display record onto viewcontroller?
Issue -2
In on selecting Profile the ProfileViewController.m is executed .On updating the user profile .It does not updates .It remain buffering .How to perform profile update .
you can download the project from this link .https://drive.google.com/file/d/1daW4veZAI21b8TqKFHauSFTboHKJceaG/view?usp=sharing
Try this:
dispatch_async(dispatch_get_main_queue(), ^{
[historyTableView reloadData];
});

NSoperation - string not visible outside the block

I am currently working on a project and using tesseract API .
The code is following :
UIImage *bwImage = [image g8_blackAndWhite];
[self.activityIndicator startAnimating];
// Display the preprocessed image to be recognized in the view
self.imageView.image = bwImage;
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] init];
operation.tesseract.engineMode = G8OCREngineModeTesseractOnly;
operation.tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;
operation.delegate = self;
operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
// Fetch the recognized text
NSString *recognizedText = tesseract.recognizedText;
NSLog(#"%#", recognizedText);
[self.activityIndicator stopAnimating];
// Spawn an alert with the recognized text
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OCR Result"
message:recognizedText
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
};
//NSLog(#"%#",);
// Finally, add the recognition operation to the queue
[self.operationQueue addOperation:operation];
}
I want to pass recognizedText string to second View controller but it is not visible outside the block.
How can I achieve this, any advice?
Declare recognizedText outside block with __block keyword to make it visible outside block.
Like below code:
......
__block NSString *recognizedText;//declared outside to make it visible outside block
operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
// Fetch the recognized text
recognizedText = tesseract.recognizedText;
NSLog(#"%#", recognizedText);
[self.activityIndicator stopAnimating];
// Spawn an alert with the recognized text
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OCR Result"
message:recognizedText
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
};
......

IOS flicker in SDK6.1 but not in 5.X or earlier

I updated my app which was created one year later for SDK 5.X, but now, I get a terrible problem.In the app, using MBProgressHUD to download files from the server, and show an UIAlerView when it finished. but, thus, the view flicker, Someone saied that the UIAlertView should disappeared on the main thread, my code is like this :
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithWindow:[[WNMAppDelegate appDelegate] window]];
hud.labelText = #"Please wait...";
[[[WNMAppDelegate appDelegate] window] addSubview:hud];
[hud showAnimated:YES whileExecutingBlock:^{
[self progressHudMethodForCheckIn:dic];
} completionBlock:^{
[hud removeFromSuperview];
[hud release];
if ([[dic allKeys] count])
{
NSString *key = [[dic allKeys] objectAtIndex:0];
[self alertMessage:[dic objectForKey:key] withTitle:key];
}
else
{
[self alertMessage:#"Succeed sign in" withTitle:#"Nice"];
}
}];
[dic release];
- (void)alertMessage:(NSString *)message withTitle:(NSString *)title{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:title
message:(NSString *)message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
can someone help me? Thank you.

NSXMLParser parserDidStartDocument freezes app

I tried to add some operations after call
- (void)parserDidStartDocument:(NSXMLParser *)parser {
//NSLog(#"found file and started parsing");
alertView = [[UIAlertView alloc] initWithTitle:#"Caricamento..."
message:#"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];
}
But it freeze the app for a while, and then, when finished the XML parse, loads the AlertView, ecc. Same thing with the UIRefreshControl. I slide down the tableView, and the app freeze while parsing, I cant see the spinner rotating.
Any idea?
Edit:
here I call the first time the parser:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString * path = #"thexmlpath.xml";
if(!caricato)
[NSThread detachNewThreadSelector:#selector(parseXMLFileAtURL:) toTarget:self withObject:path];
//[self parseXMLFileAtURL:path];
caricato = YES;}
Here I call when I use the RefreshControl:
- (void)refreshControlRequest{
NSLog(#"refreshing...");
NSString * path = #"thexmlpath.xml";
[self performSelector:#selector(parseXMLFileAtURL:) withObject:path];}
hope this will help you
- (void)parserDidStartDocument:(NSXMLParser *)parser {
//NSLog(#"found file and started parsing");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
alertView = [[UIAlertView alloc] initWithTitle:#"Caricamento..."
message:#"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];
});
dispatch_release(queue);
}

Resources