我的某個視圖保留其子視圖時遇到問題。主視圖在餐館中顯示「餐桌」,並加載子視圖以顯示此「餐桌」。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'視圖上調用。
感謝您的幫助
您是否嘗試在此應用程序上運行Analyzer?有時它可以準確檢測到保留的內容。 – ahwulf