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

解除分配主视图时,委托设置为 Nil。 我已经过度使用了 dealloc 方法来记录对我的 dealloc 调用 - 它是在"主"视图上调用的,而不是在"表"视图上调用的。

感谢您的帮助

在 dealloc 方法中尝试

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

如果你能尝试一下,那就更好

[tablevw removeFromSuperview], tablevw = nil;

我可以看到您的 tablevw 对他的代表有一周的引用,这应该足以释放父级,但只是为了争论而尝试零 tablevws 代表。创建您创建的 tablevw 实例数组(或按照已有的建议标记它们),然后在释放父实例之前将它们从 superview 中删除,将其委托设置为 nil 并终止该数组。看看会发生什么,也许这会有所帮助...

最新更新