在Iphone sdk中的ActivityIndi​​cator获取内存分配

时间:2010-03-25 07:20:00

标签: iphone objective-c

这里我在活动指示器上遇到内存分配问题,我的代码是:

- (id)init {
    if (self = [super init]) {
        self.title=@"Release Details";
        contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        contentView.backgroundColor = [UIColor clearColor];
        self.view = contentView;
        [contentView release];
        CGRect frame = CGRectMake(0,0, 320,1500);
        containerView = [[UIView alloc] initWithFrame:frame];
        webView = [ [UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        webView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"background1.png"]];
        webView.delegate = self;        
        [containerView addSubview:webView];
        CGRect activityViewframe = CGRectMake(20,8,20, 20);
        progressInd = [[UIActivityIndicatorView alloc] initWithFrame:activityViewframe];
        progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        progressInd.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |

UIViewAutoresizingFlexibleRightMargin |                                         UIViewAutoresizingFlexibleTopMargin |                                         UIViewAutoresizingFlexibleBottomMargin);             [containerView addSubview:progressInd];             [progressInd startAnimating];             progressInd.hidden = NO;             [progressInd stopAnimating];             [self.view addSubview:containerView];             isFetch = YES;         }         回归自我;     }

-(void) displayInProgressRightBarButton
{
    UIView* rightBarButtonView = [ [UIView alloc] initWithFrame:CGRectMake(270,5,45, 35)];
    [rightBarButtonView addSubview:progressInd];
    UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc]  initWithCustomView:rightBarButtonView];
    self.navigationItem.rightBarButtonItem = buttonItem;
    [rightBarButtonView release];
    [buttonItem release];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self displayInProgressRightBarButton];
    [progressInd startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [progressInd stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

此外,我在dealloc中发布了progressInd,尽管它显示了

的内存分配
progressInd = [[UIActivityIndicatorView alloc] initWithFrame:activityViewframe];

在init。

任何人都可以帮我解决这个问题。

任何人的帮助都会得到很多赞赏。

1 个答案:

答案 0 :(得分:0)

您可以(并且应该)在将其添加到容器视图后立即将其释放:

[containerView addSubview:progressInd];
[progressInd release];

容器视图本身也是如此:

[self.view addSubview:containerView];
[containerView release];

包含视图将保留子视图,因此您可以在添加后立即将其释放。