2012-01-07 65 views
0

我無法弄清楚我在做什麼錯了一個NSMustableArray。公平地說,我不太瞭解內存分配情況,所以我很抱歉,如果這是一個簡單的問題。釋放NSMutableArray?

我有一個標籤欄應用程序運行良好,並且在其中一個標籤欄上有一個人的前視圖和後視圖。

在.H我

@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文件

@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中的「釋放」之外,我已經說過保留這個數組並且沒有在其他地方釋放它。

我已經花了很多年尋找答案,但只是無法弄清楚。

謝謝!

+0

這是可可初學者必備:[可可核心能力:內存管理(http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia- CocoaCore/MemoryManagement.html#// apple_ref/doc/uid/TP40008195-CH27-SW1) – Jay 2012-01-19 13:08:15

回答

2
frontBurnsArray = [NSMutableArray arrayWithObjects: @"frontHead", @"frontChest", @"frontAbdomen", @"frontGroin",@"frontLeftArm", @"frontLeftForeArm", @"frontRightArm", @"frontRightForearm", @"frontLeftThigh", @"frontLeftLowerLeg", @"frontRightThigh", @"frontRightLowerLeg",nil]; 

您沒有使用保留財產

嘗試

self.frontBurnsArray = [NSMutableArray arrayWithObjects: @"frontHead", 
                 @"frontChest", 
                 @"frontAbdomen", 
                 //.... 
                 nil]; 

要創建陣列,即autorealesed,這意味着它會消失在下次運行循環。如果將其分配給屬性,它會自動保留並存在,直到您釋放它。你也可以撥打

frontBurnsArray = [[NSMutableArray arrayWithObjects: @"frontHead", 
                 @"frontChest", 
                 @"frontAbdomen", 
                 //.... 
                 nil] retain]; 
+0

Magic !!!但是當我將它保存在我的.h文件中時,爲什麼我需要保留它? – 2012-01-07 23:27:47

+0

你沒有使用該屬性。 – vikingosegundo 2012-01-07 23:28:16

+0

您只能通過自己進入酒店 – vikingosegundo 2012-01-07 23:28:56

相關問題