iphone - UIAlertView 按钮按下导致崩溃,而其他所有按钮都工作正常

标签 iphone uialertview uialertsheet

我有一个显示正常的警报 View 。 在我的标题中,我包含了 UIAlertViewDelegate,但由于某种原因,每当我单击警报 View 上的按钮时,我的应用程序都会因严重过量而崩溃,表示发送了无法识别的选择器。

任何想法都会有帮助。我在其他类中运行完全相同的代码,没有任何问题。

这是我的代码:

    -(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");
 }
}

所以我能弄清楚的最好的情况是,它与我从 UITableViewCell 子类调用 deletePatient 方法,并在这样做时传递患者对象这一事实有关。这是传递它的代码

-(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];
}

最佳答案

您将 PatientList 对象设置为 UIAlertView 实例的委托(delegate),然后将其释放。当用户单击警报按钮时,它会调用 [delegatealertView:self clickedButtonAtIndex:buttonIndex],但委托(delegate)病人列表已被释放并且不再存在。此时变量 delegate 包含垃圾,因此它没有 alertView:clickedButtonAtIndex: 选择器也就不足为奇了;

只需在警报alertView:clickedButtonAtIndex:方法中释放病人列表对象,或者在创建/释放外部类或仅使用属性时创建/释放病人列表:

//在*.h文件中:

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

//在 *.m 文件中: @synthesize病人列表;

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

关于iphone - UIAlertView 按钮按下导致崩溃,而其他所有按钮都工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2376668/

相关文章:

ios - 如何在 UIDatePicker 中禁用 24 小时格式

ios - UIAlertView 的标题被传递到不同屏幕上的 UITextField

iphone - UIGraphicsGetImageFromCurrentImageContext 视网膜分辨率?

iphone - 从 CIImage 获取 UIImage 无法正常工作

iphone - 在我输入时更改 UITextView 的框架

ios - 尝试在写入事务之外修改对象

ios - "iOS Would Like Access to Twitter Accounts"警报在哪里?

objective-c - 线程1 :EXC_BAd_InSTRUCTION(code =EXC_1386_INVOP,子代码)

ios - UIAlertController 崩溃 (iPad)

iphone - 使用 NSArray 指定 otherButtonTitles?