UIActivityIndi​​catorView 无响应

发布于 2025-01-07 09:46:34 字数 741 浏览 2 评论 0原文

当触发 UIButton 时,将启动 UIActivityIndi​​cator,然后在 implementLogin 完成时停止:

-(IBAction)loginButton {
    NSLog(@"loginButton triggered");

    // Checks the Fields are not empty 
    if ([sessionIdField.text length] != 0 && [usernameField.text length] != 0 ) {
        NSLog(@"Beginning Spinner");
        // Displays spinner
        [activitySpinner startAnimating];

        [self implementLogin]; 

        // Displays spinner
        [activitySpinner stopAnimating];
    }
}

但是在运行时,微调器不会出现!我已将微调器设置为“停止时隐藏”。

当我将活动指示器设置为在视图加载之前进行动画处理时,它会按应有的方式显示,因此我猜测它的 UIButton 有问题...(另外,我正在使用“Touch按钮的“内部”。)

这是一个简单的问题......任何人都可以帮忙吗?

谢谢

When a UIButton is triggered, a UIActivityIndicator is started, and then stopped when implementLogin finishes:

-(IBAction)loginButton {
    NSLog(@"loginButton triggered");

    // Checks the Fields are not empty 
    if ([sessionIdField.text length] != 0 && [usernameField.text length] != 0 ) {
        NSLog(@"Beginning Spinner");
        // Displays spinner
        [activitySpinner startAnimating];

        [self implementLogin]; 

        // Displays spinner
        [activitySpinner stopAnimating];
    }
}

However at runtime, the spinner doesn't appear! I have set the spinner to 'hide when stopped'.

When I set the activity indicator to animate before the view loads, it appears as it should, so I'm guessing it has a problem with the UIButton... (Also, I'm using 'Touch Up Inside' for the button.)

It's a simple problem... Can anyone help?

Thanks

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

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

发布评论

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

评论(2

月竹挽风 2025-01-14 09:46:34

无论 implementLogin 正在做什么(也许是发出网络请求?),它都是在主线程上执行的,这可能会阻止 UI 更新,例如旋转动画。

您可以重新编码如下内容:

[activitySpinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT, 0), ^{
    [self implementLogin]; 
    dispatch_async(dispatch_get_main_queue(), ^{
        // Stops spinner
        [activitySpinner stopAnimating];
    }
}

[代码未经测试,但您明白了。]

这里发生的情况是您将登录任务分派到后台,并且该块要做的最后一件事是停止主屏幕上的微调器线程(作为单独的任务)。

Whatever implementLogin is doing (making a network request, perhaps?), it's doing it on the main thread, which is likely blocking UI updates like spinner animation.

You could recode something like this:

[activitySpinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT, 0), ^{
    [self implementLogin]; 
    dispatch_async(dispatch_get_main_queue(), ^{
        // Stops spinner
        [activitySpinner stopAnimating];
    }
}

[Code is untested, but you get the idea.]

What is happening here is that you are dispatching the login task to the background, and the last thing that block will do is stop the spinner on the main thread (as a separate task).

梦幻的心爱 2025-01-14 09:46:34

如果没有更多代码,人们只能猜测它不起作用的原因。

我假设“Beginning Spinner”输出正确(?)

如果是这样,您可能没有正确初始化 UIActivityIndi​​catorView
看起来像这样吗?

activitySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activitySpinner.hidesWhenStopped = YES;
[view addSubview:activitySpinner];

Without more code, one can only guess the reason why it doesn't work.

I assume 'Beginning Spinner' output correctly(?)

If so, you probably didn't properly initialize the UIActivityIndicatorView.
Does it look like this?

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