在dealloc ARC之后,UIView保留了子视图

时间:2014-02-20 12:50:45

标签: ios objective-c memory-management automatic-ref-counting

我的一个观点保留其子视图时出现问题。主视图在餐馆中显示“表格”,并加载子视图以显示此“表格”。

当主视图被释放时,表似乎仍然分配给内存。我到处搜索试图找到解决方案,因为我似乎无法自己修复它。

首先是'table'视图的代码:

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

}

最后,在'main'视图中,表格的添加方式如下:

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调用给我 - 它在'main'视图上调用,但不在'table'视图上调用。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

在dealloc方法中尝试

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

如果你能尝试

会更好
[tablevw removeFromSuperview], tablevw = nil;

答案 1 :(得分:0)

我可以看到你的tablevw有一个星期引用他的委托,这应该足以释放父,但只是为了争论trie到nil tablevws委托。创建一个您创建的tablevw实例数组(或者按照已经建议的方式标记它们),然后在释放父项从superview中删除它们之前,将它们的委托设置为nil并终止该数组。看看会发生什么,也许会有所帮助...