I can’t work out what I am doing wrong with an NSMustableArray. It is fair to say that I don’t really understand memory allocation that well, so I apologize if this is a simple questions.
I have a tab bar application that is working well, and on one of the tab bars I have a front view and a back view of a person.
In the .h I have
@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
Then I have the .m file
@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
What I can’t figure out is why the NSLogs in the IBActions are both telling me that the instance has been deallocated. Apart from the “release” in the dealloc, I am have said to retain the array and have not released it elsewhere.
I have spent ages searching for an answer to this, but just can’t figure it out.
Thanks!!
You are not using the retaining property
try
You are creating an array, that is autorealesed, meaning it will disappear on the next run-loop. If you assign it to the property, it get automatically retained and will exists until you release it. you also could call