UIActivityIndi​​cator不是Animating

时间:2012-09-03 10:30:56

标签: iphone xcode uiactivityindicatorview

我在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];
}
}

3 个答案:

答案 0 :(得分:2)

编辑:从ActivityIndicator

下载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:无];

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

答案 1 :(得分:1)

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

否则你只能在-(IBAction)中延迟行动。

答案 2 :(得分:0)

我猜这些方法基于NSURLConnection。数据发送部分可能是synchronous,它会暂停主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线程会为活动指示器设置动画,同时在后台的另一个线程上提取/发布数据。

修改如果您需要自定义活动指标,请查看带有教程的thisthisthis开源活动指标文件。< / p>