i seem to be getting a EXC_BAD_ACCESS error. I have enabled NSZombies to help me track where exactly I am getting the error. My program create a grid of images from the resources in my bundle. each image is a button that my error happen when I try to printout the name of each image that was read in from my plist and saved into a custom object on in an array. my array count is equal to the total amount read in but every object in the array is delocated. would I be accidentally delocating the objects in the array while I was building my grid view.
Code:
#import "Grid_ViewViewController.h"
#import "nextImageView.h"
#import "Grid_ViewAppDelegate.h"
@implementation Grid_ViewViewController
@synthesize myScrollView;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
- (void)viewDidLoad {
[[Grid_ViewAppDelegate sharedAppDelegate] showLoadingView];
self.title = @"Products";
// Load property List data
NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:pathOfFile];
NSString *error;
NSPropertyListFormat format;
NSArray *plistArray = (NSArray*)[NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListMutableContainers
format:&format
errorDescription:&error];
productArray = [[NSMutableArray alloc] initWithCapacity:[plistArray count]];
if(plistArray) {
Products *prod;
for(NSDictionary *dict in plistArray) {
prod = [[Products alloc] initWithDict:dict];
[productArray addObject:prod];
[prod release];
}
}
// End load property list data
[super viewDidLoad];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidAppear:(BOOL)animated {
int n = [productArray count]; // Numbers of items in array;
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
int i=0, i1=0;
while(i < n) {
int yy = 4 + i1 * 79; // compute next rows y pixel level
for(int j=0; j<4; j++) {
if(i >= n)
break;
CGRect rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
Products *prod = [productArray objectAtIndex:i];
NSLog(@"Product Array item's name: %@",prod.ProductName);
UIImage *buttonImageNormal = prod.ProductPic;
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag = i; // button id representing an item in the array
// Create button callback
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:button];
//[prod release];
[button release];
i++;
}
i1 = i1+1;
}
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
-(IBAction)buttonPressed:(id)sender {
nextImageView *nextController = [[nextImageView alloc] initWithNibName:@"nextImageView" bundle:nil];
Products *prod = [productArray objectAtIndex:[sender tag]];
NSLog(@"Array Count: %d", [productArray count]);
NSLog(@"Name: %@", prod.ProductName); // PROBLEM IS HERE!!!!!!!!!
[nextController changeImage:prod];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
@end