gpt4 book ai didi

ios - 使用类方法添加和删除事件指示器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:11 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个处理简单方法的类,因此主要的 UIViewController 上的代码会更少。当我尝试从我的 UIViewController 添加和删除 UIActivityIndi​​cator 时,发生了一件非常奇怪的事情,添加 UIActivityIndi​​cator 没有问题,但是当我尝试删除它(或停止它)时,没有任何反应。

这是我的代码:

这是从主 UIViewController 调用的:

[LoadingView addLoadingView:self.activityIndicator 
andTheViewToShowIn:self.view];
[ChooseSongDataSourse reloadArrayWithData:self.arrayPlaylist
andReturn:^(NSString *resone) {
[LoadingView removeLoadingView:self.activityIndicator
andTheViewToShowIn:self.view];
[self.activityIndicator removeFromSuperview];
}];

这是从一个单独的类(“LoadingView”)完成的:

+(void)addLoadingView:(UIActivityIndicatorView *)activityIndicator
andTheViewToShowIn:(UIView *)viewToShowIn{

activityIndicator =
[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setCenter:viewToShowIn.center];
[activityIndicator startAnimating];
[viewToShowIn addSubview:activityIndicator];
}

+(void)removeLoadingView: (UIActivityIndicatorView *)activityIndicator
andTheViewToShowIn: (UIView *)viewToShowIn
{
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}

我不是在寻找一个简单的答案,我可以自己做,我是在寻求一种解释,

谢谢! (:

最佳答案

+(UIActivityIndicatorView*)addLoadingViewandTheViewToShowIn: (UIView *)viewToShowIn
{
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setCenter:viewToShowIn.center];
[activityIndicator startAnimating];
[viewToShowIn addSubview:activityIndicator];
[viewToShowIn bringSubviewToFront:activityIndicator];
return [activityIndicator autorelease];
}

+(void)removeLoadingView: (UIActivityIndicatorView *)activityIndicator
andTheViewToShowIn: (UIView *)viewToShowIn
{
[activityIndicator removeFromSuperview];
}

和:

UIActivityIndicatorView* activityIndicator = [LoadingView addLoadingViewandTheViewToShowIn:self.view];
[ChooseSongDataSourse reloadArrayWithData:self.arrayPlaylist andReturn:^(NSString *resone) {
dispatch_async(dispatch_get_main_queue(), ^(){// you can remove it, if you sure that block performed in main-thread
[LoadingView removeLoadingView:activityIndicator andTheViewToShowIn:self.view];
});
}];

你的代码的主要问题是 UIActivityIndi​​catorView 的指针没有从 +(void)addLoadingView:andTheViewToShowIn:;

返回

您可以通过返回参数或指向指针的指针从函数返回对象(就像许多通过采用 NSError** 参数返回 NSError* 的函数)。

关于ios - 使用类方法添加和删除事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com