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

发布于 2024-12-25 04:55:11 字数 1248 浏览 1 评论 0原文

我有一个在 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];

在添加滚动视图后,我有:

[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:activityIndi​​cator];, [self->ScrollView addSubview:activityIndi​​cator];, [ self.view addSubview:activityIndi​​cator];

但我就是无法显示该指标。任何人都知道可能出了什么问题?

I have a view which is created in IB. inside it I have a scroll view created programmatically. Within the scroll view I connect to a web service and fetch the content and the image. I want to show an activity indicator while doing this. So I have:

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

And right after the scrollview is added, I have:

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

And in loadimage I have:

- (void)loadImage {

    [activityIndicator startAnimating];

I've tried [self.view.window addSubview:activityIndicator];, [self->ScrollView addSubview:activityIndicator];, [self.view addSubview:activityIndicator];

but I just can't get the indicator to show. Anyone have any idea what might be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2025-01-01 04:55:11

您应该在 viewDidload 中执行此操作

- (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 方法将从互联网获取所有数据并执行其他工作。

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

You should do this in you viewDidload

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

Note: "performSelectorInBackground" will show you an activity indicator on the view and your loadScroll method will fetch all data from internet and do other work.

-(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];
}
不顾 2025-01-01 04:55:11

在多次遇到上述活动指示器问题后,到目前为止我发现了什么。

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

What I have found so far after multiple times facing above problem with activity indicator.

  • Activity indicator and the function (or method or piece of code) that is required to be executed after appearance of activity indicator both can't run on main thread. If it is so activity indicator will not appear. The solution is to run activity indicator on main thread and the function in a background thread.
  • I have also faced a case in which the function followed by activity indicator can't run in a background thread. In my case it was playing a music file using MPMoviePlayerController that can't be executed in a background thread. What I did to solve this issue was to run activity indicator in the main thread and then used an NSTimer to call my method after a delay that was good enough for activity indicator to appear and start animating.
  • I have also found that activity indicator must be called in performSelectorOnMainThread and should be added as a subview on the top view of given viewController once it starts animating.
    Once it is not needed it should be removed by calling removeFromSuperView method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文