Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在iPhone应用程序上显示活动指示器_Iphone_Objective C_Uiactivityindicatorview - Fatal编程技术网

无法在iPhone应用程序上显示活动指示器

无法在iPhone应用程序上显示活动指示器,iphone,objective-c,uiactivityindicatorview,Iphone,Objective C,Uiactivityindicatorview,我有一个在IB中创建的视图。在它里面我有一个以编程方式创建的滚动视图。在滚动视图中,我连接到web服务并获取内容和图像。我想在执行此操作时显示活动指示器。因此,我: - (void)viewDidLoad { [super viewDidLoad]; activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]

我有一个在IB中创建的视图。在它里面我有一个以编程方式创建的滚动视图。在滚动视图中,我连接到web服务并获取内容和图像。我想在执行此操作时显示活动指示器。因此,我:

- (void)viewDidLoad
{
    [super viewDidLoad];
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.center = self.view.center;
    [self.view.window addSubview:activityIndicator];
在添加scrollview之后,我有:

[self.view addSubview:scrollView];
// ADD Scroll View Ends


    //Add the Lisitng Image
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
                                        initWithTarget:self
                                        selector:@selector(loadImage) 
                                        object:nil];
    [queue addOperation:operation]; 
在loadimage中,我有:

- (void)loadImage {

    [activityIndicator startAnimating];
我尝试过[self.view.window addSubview:activityIndicator];,[自->滚动视图添加子视图:活动指示器];,[self.view addSubview:activityIndicator]


但我就是不能让指示器显示出来。有人知道什么地方出了问题吗?

你应该在你的视图中这样做

- (void)viewDidLoad
{
    //Start Activity Indicator View
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0);
    indicatorView.center = self.view.center;
    [self.view addSubview:indicatorView];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [indicatorView startAnimating];

    [self performSelectorInBackground:@selector(loadscroll) withObject:self];
}
注意:“performSelectorInBackground”将在视图上显示活动指示器,loadScroll方法将从internet获取所有数据并执行其他工作

-(void)loadscroll
{
    //Your Scroll View 
        //Your other Data Manipulation even from internet
        //When data is fetched display it 
       [self removeActivityIndicator]; //To stop activity Indicator       
}

- (void)removeActivityIndicator
{
       [indicatorView stopAnimating];
}

你应该在你的视图中这样做

- (void)viewDidLoad
{
    //Start Activity Indicator View
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0);
    indicatorView.center = self.view.center;
    [self.view addSubview:indicatorView];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [indicatorView startAnimating];

    [self performSelectorInBackground:@selector(loadscroll) withObject:self];
}
注意:“performSelectorInBackground”将在视图上显示活动指示器,loadScroll方法将从internet获取所有数据并执行其他工作

-(void)loadscroll
{
    //Your Scroll View 
        //Your other Data Manipulation even from internet
        //When data is fetched display it 
       [self removeActivityIndicator]; //To stop activity Indicator       
}

- (void)removeActivityIndicator
{
       [indicatorView stopAnimating];
}

在多次面对活动指示器的上述问题之后,我发现了什么

  • 活动指示器和活动指示器出现后需要执行的函数(或方法或代码段)都不能在主线程上运行。如果是这样,活动指示器将不会出现。解决方案是在主线程上运行ActivityIndicator,在后台线程中运行函数
  • 我还遇到过这样一种情况:活动指示器后面的函数不能在后台线程中运行。在我的例子中,它是使用MPMoviePlayerController播放一个不能在后台线程中执行的音乐文件。我解决这个问题的方法是在主线程中运行activity indicator,然后使用NSTimer在延迟后调用我的方法,该延迟足以让activity indicator出现并开始制作动画
  • 我还发现,必须在performSelectorOnMainThread中调用activity indicator,并在给定viewController开始设置动画后,将其作为子视图添加到其顶视图中。 一旦不需要它,就应该通过调用removeFromSuperView方法将其删除

在多次面对上述活动指示器问题后,我发现了什么

  • 活动指示器和活动指示器出现后需要执行的函数(或方法或代码段)都不能在主线程上运行。如果是这样,活动指示器将不会出现。解决方案是在主线程上运行ActivityIndicator,在后台线程中运行函数
  • 我还遇到过这样一种情况:活动指示器后面的函数不能在后台线程中运行。在我的例子中,它是使用MPMoviePlayerController播放一个不能在后台线程中执行的音乐文件。我解决这个问题的方法是在主线程中运行activity indicator,然后使用NSTimer在延迟后调用我的方法,该延迟足以让activity indicator出现并开始制作动画
  • 我还发现,必须在performSelectorOnMainThread中调用activity indicator,并在给定viewController开始设置动画后,将其作为子视图添加到其顶视图中。 一旦不需要它,就应该通过调用removeFromSuperView方法将其删除

在此处发布您的整个.m课程。这是因为你添加活动指示器的顺序。是的,它可能在正确的轨道上。您的UIScrollView是否具有透明背景?如果不是,那么你实际上是在掩盖UIActivityIndicatorView。您可以始终在第[self.view addSubView:scrollView]行之后;调用[self.view-bringsubview-tofront:activityIndicator];在这里发布你的整个.m课程。这是因为你添加活动指示器的顺序。是的,它可能在正确的轨道上。您的UIScrollView是否具有透明背景?如果不是,那么你实际上是在掩盖UIActivityIndicatorView。您可以始终在第[self.view addSubView:scrollView]行之后;调用[self.view-bringsubview-tofront:activityIndicator];哈立德。我已经尝试并实现了这一点,但这似乎并没有解决我的问题。我注意到的一件事是,当应用程序从后台恢复时,我可以看到指示灯。我已按照您的建议将所有代码放入loadscroll中。但活动指示器仍然不可见Vas Zorlu!它的工作,我已经首先确认,然后张贴答案。你能给我发个.m文件吗。我的电子邮件id是khalid0491@gmail.com.Thanks哈立德。我已经把它寄给你的朋友了email@SavaşZorlu检查您的电子邮件。Khalid。我已经尝试并实现了这一点,但这似乎并没有解决我的问题。我注意到的一件事是,当应用程序从后台恢复时,我可以看到指示灯。我已按照您的建议将所有代码放入loadscroll中。但活动指示器仍然不可见Vas Zorlu!它的工作,我已经首先确认,然后张贴答案。你能给我发个.m文件吗。我的电子邮件id是khalid0491@gmail.com.Thanks哈立德。我已经把它寄给你的朋友了email@SavaşZorlu检查您的电子邮件。