UIActivityIndi​​catorView总是崩溃

时间:2011-06-06 20:43:11

标签: iphone xcode uiactivityindicatorview

我的UIActivityIndi​​catorView总是崩溃我的应用程序 当我按下我的下载按钮时,指示器显示并开始旋转 但是当我停止它时,我只需触摸某个地方的屏幕并且我的应用程序崩溃了。

·H

@interface DownloadViewController : UIViewController < FinishedParsing, NSFetchedResultsControllerDelegate > 
{
    UIActivityIndicatorView* indicator;
}
@property (nonatomic, retain) UIActivityIndicatorView* indicator;

- (void)initSpinner;
- (void)spinBegin;
- (void)spinEnd;

的.m

@implementation DownloadViewController

@synthesize indicator;

- (IBAction)download:(id)sender 
{
    [self initSpinner];
    [self spinBegin];

    [OJSGatewayCommunicationService parseArticles :self];
}

- (void)initSpinner 
{
    self.indicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]autorelease];    

    // we put our spinning "thing" right in the center of the current view
    CGPoint newCenter = (CGPoint) [self.view center];
    indicator.center = newCenter;   
    [self.view addSubview:indicator];   
}

- (void)spinBegin 
{
    [indicator startAnimating];
}

- (void)spinEnd 
{
    self.indicator.hidesWhenStopped = YES;
[indicator stopAnimating];
    indicator.hidden = TRUE;
    [indicator removeFromSuperview];
}

- (void) fetchPDF:(NSMutableArray *)chapters
{
    [self spinEnd];
    ...
}

2 个答案:

答案 0 :(得分:3)

相反或自动释放它,控制它并通过在完成后调用self.indicated = nil手动释放它并在dealloc中释放它。 这样,你肯定不会在没有警告的情况下消失......

答案 1 :(得分:1)

在你的功能中:

- (void)spinEnd {
    [indicator stopAnimating];
    self.indicator = nil;
}

我建议不要将指标设置为零。实际上,设置self.indicator = nil将使指标得以释放。如果这也触发了重新分配,则在执行主循环时,UI可能无法正确地重绘自身。注意:这只是我正在做的一个假设,因为你有一种奇怪的行为。它可能有效或可能无效。

当指标停止时,我还要确保将hidesWhenStopped设置为YES。总而言之,我会改写:

- (void)spinEnd {
    self.indicator.hidesWhenStopped = YES; //-- additional guarantee, but it should already be set
    [indicator stopAnimating];
}

并在indicator中发布-dealloc

- (void)dealloc {
    ....
    [_indicator release];
    _indicator = nil;
    ...
    [super dealloc];
}

顺便说一句,还要修复initSpinner中的内存泄漏:

- (void)initSpinner {
    self.indicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];    
    ....

需要注意的是,当您执行initSpinner时,如果indicator已经存在,那么将新的UIActivityIndicatorView分配给self.indicator会使之前的hidden成为-spinEnd被释放。

编辑:

如果上述任何一个都没有用,你可以尝试另外两件事:

  1. self.indicator.hidesWhenStopped = YES中将指标[indicator stopAnimating];属性设置为YES;

  2. 我可能错了,但在调用{{1}}之前设置{{1}}可能更有效