Reputation: 783
I am using activity indicator in iphone app but problem is that when I write the line
[indicator startAnimating];
in viewDidLoad, it animates but when I write this same line in the button code where I move to next screen then it does not animate
-(IBAction)nextButtonClicked{
if ([professionLabel.text isEqualToString:@"Profession"]) {
errorLabel.text=@"Please Select the Highliteg Answers";
Q1.textColor=[UIColor redColor];
}
if ([workLabel.text isEqualToString:@"Work"]) {
errorLabel.text=@"Please Select the Highlight Answers";
Q2.textColor=[UIColor redColor];
}
if([yearLabel.text isEqualToString:@"Year"]){
errorLabel.text=@"Please Select the Highliteg Answers";
Q3.textColor=[UIColor redColor];
}
else{
errorLabel.text=@"";
Q1.textColor=[UIColor ];
Q2.textColor=[UIColor];
Q3.textColor=[UIColor];
[indicator startAnimating];
[self submitSurveyAnswers];
[self submitSurveyAnswersOne];
[self submitSurveyAnswersTwo];
OnlineViewController*targetController=[[OnlineViewController alloc]init];
targetController.mynumber=mynumber;
[self.navigationController pushViewController:targetController animated:YES];
}
}
Upvotes: 2
Views: 426
Reputation: 7644
I'm guessing the methods are NSURLConnection
based. The data sending part might be synchronous
which pauses the main UIThread
, which is why the activityIndicator does not animate.
A better way would be to move the data post part to another thread. Replace the following:
[indicator startAnimating];
[self submitSurveyAnswers];
[self submitSurveyAnswersOne];
[self submitSurveyAnswersTwo];
OnlineViewController*targetController=[[OnlineViewController alloc]init];
targetController.mynumber=mynumber;
[self.navigationController pushViewController:targetController animated:YES];
with:
[indicator startAnimating];
[NSThread detachNewThreadSelector:@selector(startPosting:) toTarget:self withObject:nil];
and create two methods:
-(void)startPosting{
[self submitSurveyAnswers];
[self submitSurveyAnswersOne];
[self submitSurveyAnswersTwo];
}
&
-(void)dataSubmissionComplete{
OnlineViewController*targetController=[[OnlineViewController alloc]init];
targetController.mynumber=mynumber;
[self.navigationController pushViewController:targetController animated:YES];
}
and then call the method dataSubmissionComplete
, when your data has been successfully submitted, on the main thread as:
[self performSelectorOnMainThread:@selector(dataSubmissionComplete) withObject:nil waitUntilDone:YES];
This process is usually followed in such situations where the UI thread animates the activity indicator while data is fetched/posted on another thread in background.
Edit- If you want custom activity indicators, take a look at this, this and this open-source activity indicator files with tutorials.
Upvotes: 0
Reputation: 38249
EDIT : Download 2 class from ActivityIndicator
Add this class file in your project. Also add QuartzCore framweork in project.
#import "SHKActivityIndicator.h" //in your .pch file of project
How use given below: to show indicator use below line.
[NSThread detachNewThreadSelector:@selector(startActivity:) toTarget:self
withObject:nil];
-(void)startActivity:(id)sender // add this method
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[SHKActivityIndicator currentIndicator] displayActivity:@"Cropping Image"];
[pool release];
}
hide any where u want like this:
[[SHKActivityIndicator currentIndicator] hide];
Add this line of code in button's event by replacing [indicator startAnimating];
[NSThread detachNewThreadSelector:@selector(startActivity:) toTarget:self withObject:nil];
Add below method which will be called:
-(void)startActivity:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[indicator startAnimating];
[pool release];
}
Upvotes: 2
Reputation: 7226
If you add this line to the -(IBAction)
of a button then it will not show the activity indicator animating because the button action is immediate and the next view will be shown almost immediately. So either you build the UIButton
programmatically and use the -(void)
function to as a selector for the UIButton
. In that case the [indicator startAnimating];
would be called then the rest of the button method.
Otherwise you could simply delay the Action in -(IBAction)
.
Upvotes: 1