這些是添加到子視圖中的代碼,在添加之前我想要檢測。
numberOfViews = [[GlobalVariable sharedInstance].itemNewsDetail count];
for (int i = 0; i < numberOfViews; i++) {
@try{
CGFloat xOrigin = i * 320;
CGRect frame;
frame.size = CGSizeMake(320, 365);
frame.origin.x = xOrigin;
frame.origin.y = 0;
detailVC = [[DetailScrollVC alloc]initWithNibName:@"DetailScrollVC" bundle:nil];
detailVC.view.frame = frame;
[detailVC loadViewByIndex:i];
UIFont *font = detailVC.txtBodyNews.font;
detailVC.txtBodyNews.font = [font fontWithSize:currentFontSize];
detailVC.txtBodyNews.tag = i;
[scrollDetail addSubview:detailVC.view];
[scrollDetail sizeToFit];
[detailVC.view release];
}@catch (NSException *exception) {
NSLog(@"ERROR HANDLING : %@",exception);
}
}
如何檢測和釋放以前添加過的子視圖?
謝謝。
遞歸判斷view所有層的子view種是否包含目標view
@interface UIView (Extend)
- (BOOL)containSubview:(UIView *)view;
@end
@implementation UIView (Extend)
- (BOOL)containSubview:(UIView *)view {
BOOL isContained = NO;
if([self.subviews containsObject:view]) {
isContained = YES;
} else {
if (self.subviews.count > 0) {
for (UIView *subview in self.subviews) {
if ([subview containSubview:view]) {
isContained = YES;
break;
}
}
}
}
return isContained;
}
@end
刪除子view
[subView removeFromSuperView];