EXC_BAD_ACCESS on iphone
I have the following:
Class Number1
.h file:
myAudiCiviliteInputViewController *civiliteInputViewController;
.m file:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewEtape4 deselectRowAtIndexPath:indexPath animated:NO];
civiliteInputViewController = [[myAudiCi开发者_运维百科viliteInputViewController alloc] init];
[self.navigationController pushViewController:civiliteInputViewController animated:YES];
[civiliteInputViewController release];
UIButton* customView = [UIButton buttonWithType: UIButtonTypeInfoLight];
[customView setFrame:CGRectMake(0, 0, 60, 31)];
[customView setImage:[UIImage imageNamed:@"nc_btn_ok.png"] forState:UIControlStateNormal];
[customView addTarget:self action:@selector(okPressed) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *envoyerButton = [[UIBarButtonItem alloc] initWithCustomView: customView];
[self.navigationController.navigationBar.topItem setRightBarButtonItem:envoyerButton];
[customView release];
}
-(void) okPressed{
self.civiliteString= civiliteInputViewController.civiliteInputString;
civiliteLabel.text = self.civiliteString;
[self.navigationController popViewControllerAnimated:YES];
}
When I click the tableView I move onto class Number 2
.
Class Number2 myAudiCiviliteInputViewController
@synthesize civiliteInputString;
- (void)dealloc
{
[civiliteInputString release];
[tabelViewCivilite release];
[tableViewArray release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tableViewArray = [[NSMutableArray alloc] initWithObjects:@"Madame", @"Mademoiselle", @"Monsieur", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
}
// Set up the cell...
[[cell textLabel] setText: [tableViewArray objectAtIndex:indexPath.row]] ;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tabelViewCivilite deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
self.civiliteInputString = selectedCell.textLabel.text;
[tableView reloadData];
}
When I go from Class Number1
to Class Number2
it works. When I come back from Class Number2
to Class Number1
I get EXC_BAD_ACCESS
in Class Number2
, at this line:
[super dealloc];
Any ideas why?
Looking at your code. UIButton* customView is autoreleased. Remove -> [customView release]
UIButton* customView = [UIButton buttonWithType: UIButtonTypeInfoLight];
[customView setFrame:CGRectMake(0, 0, 60, 31)];
[customView setImage:[UIImage imageNamed:@"nc_btn_ok.png"] forState:UIControlStateNormal];
[customView addTarget:self action:@selector(okPressed) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *envoyerButton = [[UIBarButtonItem alloc] initWithCustomView: customView];
[self.navigationController.navigationBar.topItem setRightBarButtonItem:envoyerButton];
[customView release]; // Remove this line
You shouldn't
release
customView
as it is autoreleased, remove :[customView release];
You should
release
envoyerButton
, add :[envoyerButton release];
.h file
myAudiCiviliteInputViewController *civiliteInputViewController;
.m file
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewEtape4 deselectRowAtIndexPath:indexPath animated:NO];
civiliteInputViewController = [[myAudiCiviliteInputViewController alloc] init];
[self.navigationController pushViewController:civiliteInputViewController animated:YES];
[civiliteInputViewController release];
UIButton* customView = [UIButton buttonWithType: UIButtonTypeInfoLight]; // Autoreleasing object
[customView setFrame:CGRectMake(0, 0, 60, 31)];
[customView setImage:[UIImage imageNamed:@"nc_btn_ok.png"] forState:UIControlStateNormal];
[customView addTarget:self action:@selector(okPressed) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *envoyerButton = [[UIBarButtonItem alloc] initWithCustomView: customView];
[self.navigationController.navigationBar.topItem setRightBarButtonItem:envoyerButton];
[envoyerButton release]; // You should release this object
}
-(void) okPressed
{
self.civiliteString= civiliteInputViewController.civiliteInputString;
civiliteLabel.text = self.civiliteString;
[self.navigationController popViewControllerAnimated:YES];
}
精彩评论