简体   繁体   English

UIView在取消分配ARC后保留子视图

[英]UIView is retaining subviews after dealloc ARC

I have a problem with one of my views retaining its subviews. 我对其中一个视图保留其子视图有疑问。 The main view displays 'tables' in a restaurant, and loads a subview to display this 'table'. 主视图在餐厅中显示“表”,并加载子视图以显示此“表”。

When the main view is deallocated, the tables seem to remain allocated to memory. 释放主视图后,表似乎仍分配给内存。 I have searched everywhere to try and find a solution to this as I just can't seem to fix it myself. 我到处搜索以尝试找到解决方案,因为我似乎无法自己解决。

Firstly the code for the 'table' view: 首先,“表”视图的代码:

@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

And the init method for the view: 以及视图的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;

} }

Finally, in the 'main' view the tables are added like so: 最后,在“主”视图中添加表,如下所示:

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];

The delegate is set to Nil when the main view is dealloced. 取消分配主视图时,委托设置为Nil。 I have over ridden the dealloc method to log the dealloc calls to me - it is being called on the 'main' view but not on the 'table' view. 我已经重写了dealloc方法来记录对我的dealloc调用-它是在“主”视图上而不是在“表”视图上调用的。

Thanks for your help 谢谢你的帮助

In the dealloc method try 在dealloc方法中尝试

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

It would be even better if u can try 如果你可以尝试的话会更好

[tablevw removeFromSuperview], tablevw = nil;

I can see that your tablevw has a week reference to his delegate and that should be enough for to release the parent, but just for argue sake trie to nil tablevws delegate. 我可以看到您的tablevw有一个星期参考其委托人,这应该足以释放父级,但是为了争辩而把nil tablevws委托给零。 Create an array of tablevw instances you created (or tag them as already suggested), then before you release parent remove them from superview, set their delegate to nil and kill the array. 创建一个创建的tablevw实例数组(或按照建议的那样对其进行标记),然后在释放父级之前,将它们从超级视图中删除,将其委托设置为nil并杀死该数组。 See what happenes, maybe that will help... 看看会发生什么,也许会有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM