iPhone UIActivityIndi​​cator 在进程完成之前不会显示

发布于 2024-12-23 14:52:53 字数 1182 浏览 0 评论 0原文

我已经研究了几天,但没有发现任何有效的方法。

这是所需的过程:用户在弹出窗口上按 Enter 按钮 -> ActivityIdicator出现->保存过程发生->活动指示器消失。

但是,由于某种原因,ActivityIndi​​cator 直到该过程完成后才显示,使其完全无用。

我尝试按照此处描述的过程进行操作: UIActivityIndi​​cator 无法正常工作?

以下是弹出窗口

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the Enter/Cancel buttons
    [self performSelector:@selector(DisplaySpinner) withObject:nil afterDelay:0];
    if (buttonIndex == 1)
    {
        [self performSelector:@selector(EnterButtonClicked) withObject:nil afterDelay:0];
    }
    else
    {
        NSLog(@"Name Cancel Clicked");
    }
    [NameField resignFirstResponder];
    }

下面是 DisplaySpinner 方法的代码:

-(void)DisplaySpinner{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    [self.view addSubview:loadingIndicator];
    [loadingIndicator startAnimating];

    [pool release];
}

EnterButtonClicked 方法包含保存过程。尽管在单独的进程中运行,但 ActivityIndi​​cator 直到进程完成后才会显示。

有什么建议吗?

I've been researching this for a few days, but nothing I've found works.

Here's the desired process: User presses the enter button on a pop-up window -> ActivityIdicator appears -> saving process occurs -> ActivityIndicator disappears.

However, for some reason the ActivityIndicator does not show up until after the process is complete, rendering it completely useless.

I attempted to follow the process described here: UIActivityIndicator not working properly?

Here is the code for the pop-up window

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the Enter/Cancel buttons
    [self performSelector:@selector(DisplaySpinner) withObject:nil afterDelay:0];
    if (buttonIndex == 1)
    {
        [self performSelector:@selector(EnterButtonClicked) withObject:nil afterDelay:0];
    }
    else
    {
        NSLog(@"Name Cancel Clicked");
    }
    [NameField resignFirstResponder];
    }

Here is the code for the DisplaySpinner method:

-(void)DisplaySpinner{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

    [self.view addSubview:loadingIndicator];
    [loadingIndicator startAnimating];

    [pool release];
}

The EnterButtonClicked Method contains the saving process. Despite running in seperate processes, The ActivityIndicator doesn't show up until after the process is complete.

Any suggestions?

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

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

发布评论

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

评论(1

旧城空念 2024-12-30 14:52:53

在主运行循环重新获得控制之前,应用不会更新屏幕以显示 UIActivityIndi​​catorView。如果您的处理任务阻塞主线程,则在完成之前不会发生 UI 更新。您应该异步进行处理。

当发生旋转事件时,将通过主运行循环一次性调用 willRotate...willAnimateRotation... 方法。因此,您可以在显示活动指示器之前阻止该方法。

为了完成这项工作,您需要将方法任务推送到另一个线程。当工作完成时,该方法将回调该视图控制器,以便可以更新视图。我会将活动指示器显示在 willAnimateRotation... 方法中。

您需要做的是强制 UIActivityIndi​​catorView 开始显示,即使运行循环不会结束。一种方法是 -

[self performSelector:@selector(animation) withObject:nil afterDelay:0]

-(void)startSpinner
{
    NSAutoreleasepool *pool = [[NSAutorepleasepool alloc]init];
    [indicatorView startAnimating];
    [pool release];
}

所以本质上,performSelector 设置一个计时器来在当前线程的运行循环上执行动画消息。计时器配置为在默认模式 (NSDefaultRunLoopMode) 下运行。当计时器触发时,线程尝试将消息从运行循环中出队并执行选择器。如果运行循环正在运行并且处于默认模式,则成功;否则,计时器将等待,直到运行循环处于默认模式。

请注意,指定延迟 0 并不一定会导致选择器立即执行。选择器仍然在线程的运行循环中排队并尽快执行。

The app doesn't update the screen to show the UIActivityIndicatorView until the main run loop regains control. If your processing task blocks the main thread, the no UI updates will take place until it is finished. You should do your processing asynchronously.

When a rotation event happens, the willRotate... and willAnimateRotation... methods are called in one pass through the main run loop. So you block on the method before displaying the activity indicator.

To make this work, you need to push the method task over to another thread. That method would call back to this view controller when the work is completed so the view can be updated. I would put show the activity indicator in the willAnimateRotation... method.

What you need to do is forcing the UIActivityIndicatorView to start displaying even though the run loop won't be ended. One way is -

[self performSelector:@selector(animation) withObject:nil afterDelay:0]

-(void)startSpinner
{
    NSAutoreleasepool *pool = [[NSAutorepleasepool alloc]init];
    [indicatorView startAnimating];
    [pool release];
}

So essentially, performSelector sets up a timer to perform the animation message on the current thread’s run loop. The timer is configured to run in the default mode (NSDefaultRunLoopMode). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in the default mode; otherwise, the timer waits until the run loop is in the default mode.

Please note that specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文