我有一個應用程序,顯示9 UIImageViews,我添加的能力,用戶刪除它們。代碼工作正常,除了我在界面構建器中使用標記有一些問題。看來我的代碼根據它在數組中的位置刪除ImageView中的圖像。所以,如果我有3周的UIImageViews,我會在第一UIImageViews的標籤第二圖像視圖的標籤設置爲1,2,第三至3。我先嚐試刪除第三個,我也得到了錯誤: 終止由於應用程序未捕獲的異常NSRangeException,原因: * ' - [__ NSArrayM removeObjectAtIndex:]:索引3超出範圍[0..1]'問題與Xcode中的標籤
這是我正在使用的代碼:
- (IBAction)deleteButtonPressed:(id)sender {
NSLog(@"Sender is %@", sender);
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[deleteAlertView show];
int imageIndex = ((UIButton *)sender).tag;
deleteAlertView.tag = imageIndex;
}
- (void)alertView: (UIAlertView *) alertView
clickedButtonAtIndex: (NSInteger) buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
NSLog(@"The tag is %i", alertView.tag);
[self.array removeObjectAtIndex: alertView.tag];
NSLog(@"After deleting item, array count = %d", [array count]);
NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
我必須把第一個圖像中的0指數的陣列,但我知道,你不能有一個標記爲0,因爲它是默認標記。所以,我不知道如何解決這個問題。任何幫助非常感謝,謝謝!
非常感謝您的回答。通過數組遍歷並返回具有匹配聲音的標籤的項目將是完美的,但我不知道如何編寫該代碼。對不起,我對Objective-C仍然很陌生。 – John 2012-04-05 18:45:10
我已經用示例更新了答案。 **不要相信它,因爲我只是猜測你的一些變量是如何定義的。但它應該是一個好的起點。 – 2012-04-05 19:02:12
非常感謝你!如果我將NSData對象放在數組中,而不是直接在UIImageView中,這種方法是否可以工作?再次感謝! – John 2012-04-05 21:41:46