2

我在 iphone 应用程序中使用活动指示器,但问题是当我写行时

 [indicator startAnimating];

在 viewDidLoad 中,它会产生动画,但是当我在按钮代码中写下同一行时,我会移至下一个屏幕,然后它不会产生动画

-(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];
}
}
4

3 回答 3

2

编辑:从ActivityIndi ​​cator 下载 2 类

Add this class file in your project. Also add QuartzCore framweork in project.

#import "SHKActivityIndicator.h" //in your .pch file of project

下面给出了如何使用:显示指标在下方的使用。

    [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];
}

像这样隐藏任何你想要的地方:

[[SHKActivityIndicator currentIndicator] hide];

通过替换 [indicator startAnimating]; 在按钮的事件中添加这行代码

[NSThread detachNewThreadSelector:@selector(startActivity:) toTarget:self withObject:nil];

添加以下将被调用的方法:

-(void)startActivity:(id)sender
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   [indicator startAnimating];
   [pool release];
}
于 2012-09-03T10:37:56.847 回答
1

如果将此行添加到-(IBAction)按钮,则它不会显示活动指示器动画,因为按钮操作是立即的,并且下一个视图将几乎立即显示。因此,要么您以UIButton编程方式构建并使用该-(void)函数作为UIButton. 在这种情况下,[indicator startAnimating];将调用按钮方法的其余部分。

否则,您可以简单地延迟-(IBAction).

于 2012-09-03T10:44:43.717 回答
0

我猜这些方法是NSURLConnection基于的。数据发送部分可能会synchronous暂停 main UIThread,这就是 activityIndi​​cator 没有动画的原因。

更好的方法是将数据发布部分移动到另一个线程。替换以下内容:

[indicator startAnimating];
[self submitSurveyAnswers];

[self submitSurveyAnswersOne];
[self submitSurveyAnswersTwo];

OnlineViewController*targetController=[[OnlineViewController alloc]init];
targetController.mynumber=mynumber;
[self.navigationController pushViewController:targetController animated:YES];

和:

[indicator startAnimating];
[NSThread detachNewThreadSelector:@selector(startPosting:) toTarget:self withObject:nil];

并创建两个方法:

-(void)startPosting{
  [self submitSurveyAnswers];
  [self submitSurveyAnswersOne];
  [self submitSurveyAnswersTwo];
}

&

-(void)dataSubmissionComplete{
  OnlineViewController*targetController=[[OnlineViewController alloc]init];
  targetController.mynumber=mynumber;
  [self.navigationController pushViewController:targetController animated:YES];
}

然后dataSubmissionComplete在主线程上调用该方法,当您的数据已成功提交时:

[self performSelectorOnMainThread:@selector(dataSubmissionComplete) withObject:nil waitUntilDone:YES];

在 UI 线程为活动指示器设置动画同时在后台的另一个线程上获取/发布数据的情况下,通常会遵循此过程。

编辑 -如果您想要自定义活动指示器,请查看开源活动指示器文件和教程。

于 2012-09-03T12:49:59.350 回答