为什么 UIIndicator 不移走?

发布于 2024-12-18 03:05:50 字数 910 浏览 4 评论 0原文

我在我的应用程序中使用 UIActivityIndi​​cator。我为它编写了如下代码:

-(void)startSpinner {
    
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.hidden = NO;
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [self.view bringSubviewToFront:spinner];
    [spinner startAnimating];
}

我在 UIButton 的操作事件上调用此方法,并删除指示器,我编写代码如下:

-(void)stopSpinner {
    
    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
}

单击按钮指示器出现,但当我在视图中调用 -(void)stopSpinner 方法时 willAppear 指示器不会消失。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self stopSpinner];
   
}

即使我调试代码,我发现控制权也转到了 stopSpinner()

这里有什么问题呢?

I use UIActivityIndicator in my app. I have written code for it as follows:

-(void)startSpinner {
    
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.hidden = NO;
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [self.view bringSubviewToFront:spinner];
    [spinner startAnimating];
}

I call this method on the UIButton's action event, and to remove indicator I write the code as follows:

-(void)stopSpinner {
    
    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
}

on click on the button indicator appears but when I call -(void)stopSpinner method in view
willAppear the indicator does not disapppear.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self stopSpinner];
   
}

Even I debug the code and I found that control also goes to the stopSpinner().

What is the problem here?

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

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

发布评论

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

评论(1

萌无敌 2024-12-25 03:05:50

startSpinner 中可能存在潜在泄漏,因为您始终创建 UIActivityIndi​​catorView 而不释放它。像这样更改您的方法:

-(void)startSpinner {
    if (spinner){
        [spinner removeFromSuperview];
        [spinner release]; spinner = nil;
    }
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [spinner startAnimating];
}

要停止动画,请为另一个 UIButton 操作分配 stopSpinner 。因为 viewWillAppear 会在您点击任何按钮之前触发。
附:也许你的意思是viewWillDisappear

You might have potential leak in startSpinner because you always create UIActivityIndicatorView without releasing it. Change your method like this:

-(void)startSpinner {
    if (spinner){
        [spinner removeFromSuperview];
        [spinner release]; spinner = nil;
    }
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [spinner startAnimating];
}

For stopping animation assign stopSpinner for another UIButton action. Cos viewWillAppear will fired early then you tap on any button.
ps. maybe you mean viewWillDisappear?

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