0

我的某個視圖保留其子視圖時遇到問題。主視圖在餐館中顯示「餐桌」,並加載子視圖以顯示此「餐桌」。UIView在dealloc之後保留子視圖ARC

當主視圖被釋放時,表似乎仍然分配給內存。我到處搜索,試圖找到解決辦法,因爲我似乎無法自己修復它。

首先爲 '表' 視圖的代碼:

@protocol tableDelegate <NSObject> 
@required 
- (void)tablePress:(id)sender; 
- (void)tableSelect:(id)sender; 

@end 

@interface tablevw : UIView 
{ 
    CGPoint currentPoint; 
} 

-(void)changeOrderImage; 
-(id)initWithFrame:(CGRect)frame teststring:(NSString *)ts; 

@property (nonatomic, weak) IBOutlet UIView *view; 
@property (nonatomic, weak) IBOutlet UILabel *numberLabel; 
@property (nonatomic, weak) IBOutlet UILabel *nameLabel; 
@property (nonatomic) BOOL isEdit; 
@property (nonatomic) BOOL hasOrder; 
@property (nonatomic, strong) NSMutableDictionary * orderNumbers; 
@property (nonatomic) int orderNumber; 
@property (nonatomic, strong) NSString *teststring; 
@property (nonatomic, weak) IBOutlet UIImageView *tableImage; 
@property (nonatomic, weak) id<tableDelegate> delegate; 

@end 

而對於視圖init方法:

- (id)initWithFrame:(CGRect)frame teststring:(NSString *)ts{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     orderNumbers = [[NSMutableDictionary alloc]init]; 

     isRemote = FALSE; 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"tabletopRound" ofType:@"png"]; 

     UIImageView * iV =[[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:filePath]]; 

     self.tableImage= iV; 


     tableImage.frame = CGRectMake(0, 0, 121, 121); 

     locked = FALSE; 

     [self addSubview:tableImage]; 


     UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)]; 

     doubleTapGestureRecognizer.numberOfTapsRequired = 2; 


     [self addGestureRecognizer:doubleTapGestureRecognizer]; 

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(35, 50, 48, 20)]; 

     numberLabel = label; 
     [numberLabel setTextColor:[UIColor blackColor]]; 
     [numberLabel setBackgroundColor:[UIColor clearColor]]; 
     [numberLabel setText:ts]; 
     [self addSubview:numberLabel]; 


    UILabel * labelTwo = [[UILabel alloc] initWithFrame:CGRectMake(24, 123, 70, 20)]; 
    nameLabel = labelTwo; 
    [nameLabel setTextColor:[UIColor whiteColor]]; 
    [nameLabel setBackgroundColor:[UIColor clearColor]]; 
    [self addSubview:nameLabel]; 

    self.userInteractionEnabled = YES; 

self.tag = [ts intValue]; 

} 


return self; 

}

最後,在 '主' 查看錶格如下添加:

NSString *myString = [results stringForColumn:@"table_number"]; 

tablevw * tableView = [[tablevw alloc] initWithFrame:CGRectMake(x-60.5, y-60.5, 121, 121) teststring:myString]; 
tableView.delegate = self; 
[self.view addSubview: tableView]; 

在處理主視圖時,委託設置爲無。我已經駕馭了dealloc方法來記錄對我的dealloc調用 - 它在'main'視圖上被調用,但不在'table'視圖上調用。

感謝您的幫助

+0

您是否嘗試在此應用程序上運行Analyzer?有時它可以準確檢測到保留的內容。 – ahwulf

回答

0

在dealloc方法嘗試

for (UIView *view in [self.view subviews]) { 
    [view removeFromSuperview]; 
} 

它甚至會更好,如果ü可以嘗試

[tablevw removeFromSuperview], tablevw = nil; 
+0

謝謝,對不起,我忘了說,我已經試過這個:'[[self.view子視圖] makeObjectsPerformSelector:@selector(removeFromSuperview)];' – larrysanchez

+0

嗨,我已經將您的方法添加到dealloc方法的主要觀點,儘管它仍然保留着這些觀點。 – larrysanchez

+0

嗨,我已經嘗試過你的編輯代碼,但它不會讓我嘗試tablevw = nil;因爲我必須通過子視圖來列舉這些。 – larrysanchez

0

我可以看到你的tablevw有一個星期參考給他的代表,這應該足以釋放父母,但僅僅是爲了爭取清零代表。創建一個你創建的tablevw實例的數組(或者如已經建議的那樣標記它們),然後在釋放父級將它們從超級視圖中移除之前,將它們的委託設置爲零並殺死該數組。看看發生了什麼,也許這會有所幫助...