[英]Remove from superview
只是一個小問題,我真的有很多問題
基本上,我正在做的就是每次按下一個能正常工作的按鈕時進行查看。 當我想刪除我從超級視圖中刪除時創建的所有圖像時,它只會刪除堆棧中的最后一個。 有什么方法可以擺脫我制作的所有圖像嗎?
這是代碼
這會將圖片顯示在屏幕上
- (IBAction)pushBn:(id)sender {
ZeldaView *newZelda = [[ZeldaView alloc]initWithNibName:@"ZeldaView" bundle:nil];
theZeldaView=newZelda;
[self.view insertSubview:theZeldaView.view atIndex:1];
}
這會在我觸摸時將其刪除
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[touches anyObject];
CGPoint location=[touch locationInView:theZeldaView.view];
if (CGRectContainsPoint(theZeldaView.theImage.frame, location)) {
[theZeldaView.view removeFromSuperview];
}
}
當然,只需遍歷父視圖(self.view)的子級並刪除任何ZeldaView元素即可。
最簡單的說是:
for (UIView* subView in [self.view.subviews])
{
if ([subView isKindOfClass:[ZeldaView class]])
[subView removeFromSuperview];
}
盡管您可能希望對此進行擴展,以使其在迭代過程中不執行實際的刪除操作,但您可能希望使用responsesToSelector和自定義方法,而不是在此處檢查該類,以便您可以在ZeldaView類中進行所需的任何清理。 希望這對您有意義。
當然,有很多方法可以做到這一點。 我會保留一個ZeldaView
對象數組,當您要刪除它們時,遍歷該數組並將其全部刪除。
在您的.h中:
@property (nonatomic, retain) NSMutableArray *zeldaViews;
當您添加ZeldaView時:
// create a newZeldaView and add it to the superview
[self.zeldaViews addObject:newZeldaView];
當您想全部刪除它們時:
for (ZeldaView *zeldaView in self.zeldaViews) {
[zeldaView.view removeFromSuperview];
}
[self.zeldaViews removeAllObjects];
在viewDidLoad中創建可變數組,並在viewDidUnload和dealloc中釋放它。 如果使用ARC,則修改為適當。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.