我嘗試使用下面的代碼從現有視圖中刪除圖像:removeFromSuperview的UIImageView不工作
-(void) deleteImage:(int)imageID{
//[self.imageArray removeObjectAtIndex:imageID];
//remove the image from the screen
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UIImageView class]] && [view tag] == imageID) {
//could be a bug here with the re-Ordering of the array (could add a helper method to reset all the tags on screen when this is called
NSLog(@"view %@", view);
//[self.imageArray removeObjectAtIndex:imageID];
[view removeFromSuperview];
}
}
}
的NSLog的輸出如下:
view <UIImageView: 0x18b8d7b0; frame = (0 0; 768 2016); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x18b8d890>>
的問題我已經是它似乎獲取ImageView但它不會將其刪除。
請你詳細說明一下這個要求。你真的需要刪除'imageView'本身或者只是清除'imageView'顯示的圖像嗎? – GoGreen
@GoGreen我需要刪除imageView本身 – jampez77
在將它從superView中刪除之前,您已經記錄了該語句。嘗試循環遍歷for語句後的子視圖來檢查「imageView」是否仍然存在。另外,這不是刪除子視圖的理想方式。通過將其添加爲「屬性」或「實例變量」,保留對要刪除的視圖的引用。然後您可以使用此參考將其刪除。請再檢查一次。 – GoGreen