程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-IOS中如何釋放subView

ios-IOS中如何釋放subView

編輯:編程綜合問答
IOS中如何釋放subView

這些是添加到子視圖中的代碼,在添加之前我想要檢測。

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];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved