views:

328

answers:

3

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];
}
A: 

Everything is fine with the code you provided. Unless something funky is happening with the patient object somewhere else, I'd say all looks good here.

Brad Smith
Hmmm. I think the patient object is alright, since in the deletePatient method '//Do something else' actually deletes the object fine. The problem is specifically with the Alert.Do you know of any conflicts that Alert view might have with other classes? Just a thought.
monotreme
A: 

Try using UIAlerView as autoreleased object like this;

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:alertString message:nil delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No",nil] autorelease];
[alert1 show];
EEE
Nope. That Didn't work.I tried a few other things and here's the deal:I'm evoking the method the deletePatient method from my UITableViewCell subclass through an IBAction button press, passing along the object patient in the process. When I just use the accessoryTapped delegate method, no problems, so it must be something with the subclass. I'll add the invocation of that above.
monotreme
+2  A: 

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];
Vladimir
That was the problem! Thanks so much!
monotreme