首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >释放NSMutableArray?

释放NSMutableArray?
EN

Stack Overflow用户
提问于 2012-01-08 07:19:58
回答 1查看 556关注 0票数 0

我不知道我用NSMustableArray做错了什么。公平地说,我对内存分配不是很了解,所以如果这是一个简单的问题,我很抱歉。

我有一个工作正常的选项卡栏应用程序,在其中一个选项卡栏上,我有一个人的前视图和后视图。

在.h中,我有

代码语言:javascript
复制
@interface BurnsCalculatorViewController : UIViewController// <UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIView              *frontView;
    UIView              *backView;
    UIButton            *frontButton;
    UIButton            *backButton;
    NSMutableArray      *frontBurnsArray;
}

@property (nonatomic, retain) UIView            *frontView;
@property (nonatomic, retain) UIView            *backView;
@property (nonatomic, retain) UIButton          *frontButton;
@property (nonatomic, retain) UIButton          *backButton;
@property (nonatomic, retain) NSMutableArray    *frontBurnsArray;

-(IBAction)frontButtonSelect:(id)sender;
-(IBAction)backButtonSelect:(id)sender;

@end

然后我就有了.m文件

代码语言:javascript
复制
@implementation BurnsCalculatorViewController

@synthesize frontView;
@synthesize backView;
@synthesize frontButton;
@synthesize backButton;
@synthesize frontBurnsArray;

-(IBAction)frontButtonSelect:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
    [self.view addSubview:backView];
    [self.view addSubview:backButton];
[UIView commitAnimations];
    NSLog(@"%@", frontBurnsArray);
}

-(IBAction)backButtonSelect:(id)sender
{
    [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
    [self.view addSubview:frontView];
    [self.view addSubview:frontButton];
[UIView commitAnimations];
    NSLog(@"%@", frontBurnsArray);
}

-(void)viewDidLoad
{
    frontBurnsArray = [NSMutableArray arrayWithObjects: @"frontHead", @"frontChest", @"frontAbdomen", @"frontGroin",@"frontLeftArm", @"frontLeftForeArm", @"frontRightArm", @"frontRightForearm", @"frontLeftThigh", @"frontLeftLowerLeg", @"frontRightThigh", @"frontRightLowerLeg",nil];

    CGRect viewframe = CGRectMake(0, 0, 320, 480);
    frontView = [[UIView alloc] initWithFrame:viewframe];
    frontView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:frontView];

    frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [frontButton addTarget:self action:@selector(frontButtonSelect:) forControlEvents:UIControlEventTouchDown];
    [frontButton setTitle:@"Show Back" forState:UIControlStateNormal];
    frontButton.frame = CGRectMake(210, 10, 100, 30);
    [self.view addSubview:frontButton];

    backView = [[UIView alloc] initWithFrame:viewframe];
    backView.backgroundColor = [UIColor blackColor];
    backButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [backButton addTarget:self action:@selector(backButtonSelect:)forControlEvents:UIControlEventTouchDown];
    [backButton setTitle:@"Show Front" forState:UIControlStateNormal];
    backButton.frame = CGRectMake(210, 10, 100, 30);
}

-(void)dealloc
{
    [super dealloc];
    [frontButton release];
    [backButton release];
    [frontBurnsArray release];
}
@end

我不明白的是,为什么IBActions中的NSLogs都告诉我实例已被释放。除了在dealloc中的“释放”之外,据说我保留了数组,并且没有在其他地方释放它。

我花了很长时间寻找这个问题的答案,但就是找不到答案。

谢谢!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-08 07:23:52

代码语言:javascript
复制
frontBurnsArray = [NSMutableArray arrayWithObjects: @"frontHead", @"frontChest", @"frontAbdomen", @"frontGroin",@"frontLeftArm", @"frontLeftForeArm", @"frontRightArm", @"frontRightForearm", @"frontLeftThigh", @"frontLeftLowerLeg", @"frontRightThigh", @"frontRightLowerLeg",nil];

您没有使用保留属性

试一试

代码语言:javascript
复制
self.frontBurnsArray = [NSMutableArray arrayWithObjects: @"frontHead", 
                                                         @"frontChest",
                                                         @"frontAbdomen", 
                                                         //....
                                                         nil];

您正在创建一个自动实现的数组,这意味着它将在下一个运行循环中消失。如果将其分配给属性,它将自动保留并将一直存在,直到您释放它为止。您也可以调用

代码语言:javascript
复制
frontBurnsArray = [[NSMutableArray arrayWithObjects: @"frontHead", 
                                                         @"frontChest",
                                                         @"frontAbdomen", 
                                                         //....
                                                         nil] retain];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8773959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档