2010-03-04 48 views
3

我有一個alertview顯示罰款。 在我的頭文件中,我已經包含了UIAlertViewDelegate,但由於某種原因,每當我單擊警報視圖上的按鈕時,我的應用程序崩潰時會出現一個不合理的過量,並說發送了無法識別的選擇器。UIAlertView按鈕導致崩潰,當所有其他工作正常

任何想法都會有所幫助。我有完全相同的代碼在其他類中運行,完全沒有問題。

這裏是我的代碼:

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

什麼是無法識別的選擇器簽名? – executor21 2010-03-04 03:26:53

+0

我在控制檯得到的錯誤是這樣的: - [NSCFType alertView:clickedButtonAtIndex:]:無法識別的選擇器發送到實例0x1766400' – monotreme 2010-03-04 05:21:20

回答

8

您將patientList對象設置爲UIAlertView實例的委託,然後將其釋放。當用戶點擊警報按鈕時,它會調用[委託alertView:self clickedButtonAtIndex:buttonIndex],但委託,patientList已經被釋放並且不再存在。此時變量代表包含垃圾,所以不會驚慌它沒有alertView:clickedButtonAtIndex:選擇器;

剛剛發佈patientList對象警報alertView:clickedButtonAtIndex:方法,或做patientList創建/當你創建/釋放外類或簡單地使用財產釋放:

//在* .h文件中:

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

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

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

這就是問題所在!非常感謝! – monotreme 2010-03-04 18:41:38

+0

非常有幫助,清楚,直接。謝謝! (: – Neeku 2013-11-08 16:57:11

0

一切都很好,你提供的代碼。除非患者對象在別的地方發生了一些奇怪的事情,否則我會說在這裏看起來很好。

+0

嗯。我認爲病人對象是正常的,因爲在deletePatient方法中'//做別的事情'實際上刪除了對象。這個問題與Alert相關。 您是否知道Alert視圖可能與其他類有衝突?只是一個想法。 – monotreme 2010-03-04 05:56:50

0

嘗試使用UIAlerView作爲自動釋放對象,像這樣;

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:alertString message:nil delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No",nil] autorelease]; 
[alert1 show]; 
+0

沒有。那沒用。 我嘗試了一些其他的東西,這裏的交易: 我通過一個IBAction按鈕,從我的UITableViewCell子類喚起方法deletePatient方法,傳遞過程中的對象患者。當我只使用accessoryTapped委託方法時,沒有問題,所以它必須與子類相關。我將添加上面的調用。 – monotreme 2010-03-04 07:10:36