Search code examples
iphoneuialertviewuialertsheet

UIAlertView button press causing crash, when all others work fine


I have an alertview which displays fine. In my header I've included the UIAlertViewDelegate, but for some reason whenever I click a button on the alert view my app crashes with a bad excess, saying that an unrecognized selector was sent.

Any ideas would be helpful. I have the exact same code running in other classes with no problem at all.

Here's my code:

    -(void)deletePatient
{
 NSLog(@"Delete");
 //Patient *patientInRow = (Patient *)[[self fetchedResultsController] objectAtIndexPath:cellAtIndexPath];
 NSMutableArray *visitsArray = [[NSMutableArray alloc] initWithArray:[patient.patientsVisits allObjects]];
 //cellAtIndexPath = indexPath;
 int visitsCount = [visitsArray count];
 NSLog(@"Visit count is %i", visitsCount);
 if (visitsCount !=0) 
 {
  //Display AlertView
  NSString *alertString = [NSString stringWithFormat:@"Would you like to delete %@'s data and all their visits and notes?", patient.nameGiven];
  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:alertString message:nil delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No",nil];
  [alert1 show];
  [alert1 release];

 }
 else if (visitsCount ==0) 
 {
  //Do something else
 }

 [visitsArray release];

}

-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 // the user clicked one of the OK/Cancel buttons
 if (buttonIndex == 0)
 {
  NSLog(@"Yes");

 }
 else
 {
  NSLog(@"No");
 }
}

So the best I can figure it out, its related to the fact I'm calling the deletePatient method from my UITableViewCell subClass, and passing along the patient object as I do so. Here's the code for passing it along

-(IBAction)deletePatient:(id)sender
{
    NSLog(@"Delete Patient:%@",patient.nameGiven);
    PatientListTableViewController *patientList = [[PatientListTableViewController alloc] init];
    patientList.patient = patient;
    UITableView *tableView = (UITableView *)[self superview];
    tableView.scrollEnabled = YES;
    [patientList deletePatient];
    menuView.center = CGPointMake(160,menuView.center.y);
    [patientList release];
}

Solution

  • You set patientList object as delegate of UIAlertView instance, then released it. When user clicks alert button it calls [delegate alertView:self clickedButtonAtIndex:buttonIndex], but the delegate, patientList was already released and no longer exists. The variable delegate at this moment contain garbage, so not surprizing it have no alertView:clickedButtonAtIndex: selector;

    Just release patientList object in alert alertView:clickedButtonAtIndex: method, or do patientList creating/releasing when you create/release outer class or simply use property:

    // in *.h file:

    ...
    PatientListTableViewController *patientList;
    ...
    @property(retain) PatientListTableViewController *patientList;
    ...
    

    // in *.m file: @synthesize patientList;

    ...
    self.patientList =  [[PatientListTableViewController alloc] init];