我有一個UITableView
加載數據從CoreData
。如果我選擇刪除表中的記錄,我使用:傳遞NSIndexPath到alertView clickedButtonAtIndex
[self.managedObjectContext deleteObject:[self.recordList objectAtIndex:indexPath.row]];
[self.managedObjectContext save:nil];
[self.tableViewSavedRecords reloadData];
並且一切正常 - 記錄被刪除並重新加載表。
然而有些紀錄是密碼保護,當選擇刪除這種記錄警報出現要求輸入密碼:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Warning" message: @"This record has been password protected.\nEnter password." delegate: self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
的alertView clickedButtonAtIndex
:
NSString *enteredPassword = [[alertView textFieldAtIndex:0] text];
if ([enteredPassword isEqualToString: savedPassword]) {
[self.managedObjectContext deleteObject:[self.recordList objectAtIndex:indexPath.row]];
[self.managedObjectContext save:nil];
[self.tableViewSavedRecords reloadData];
}
的問題是objectAtIndex:indexPath.row
是顯然在alertView clickedButtonAtIndex
部分未被識別。我如何通過NSIndexPath
到alertView clickedButtonAtIndex
?
我曾嘗試聲明:
NSIndexPath *selectedIndexPath;
然後,如果觸發警報我已經包括:
selectedIndexPath = [tableViewPausedObs indexPathForSelectedRow];
然後在AlertView點:
[self.managedObjectContext deleteObject: selectedIndexPath.row];
[self.managedObjectContext save:nil];
但它刪除表的第一行,而不是選定的行。
隨時隨地獲取選定行的NSIndexPath'UIAlertView'已經過時了幾年。使用基於塊的'UIAlertController',並且可以捕獲索引路徑。 – vadian