1

初期化時に既にカウント 2 を保持している UIView があり、その理由がわかりません。その結果、removefromsuperview で削除できません。

ViewController.h

  @property (nonatomic, retain)FinalAlgView * drawView;

ViewController.m

  self.drawView =[[FinalAlgView alloc]init];

 NSLog(@"the retain count 1 of drawView is %d", [self.drawView retainCount]);
 //the retain count 1 of drawView is 2

 [self.bookReader addSubview:self.drawView];

 NSLog(@"the retain count 2 of drawView is %d", [self.drawView retainCount]);
 //the retain count 2 of drawView is 3

 [self.drawView release];

 NSLog(@"the retain count 3 of drawView is %d", [self.drawView retainCount]);
 //the retain count 3 of drawView is 2

 [UIView animateWithDuration:0.2
                 animations:^{self.drawView.alpha = 0.0;}
                 completion:^(BOOL finished){ [self.drawView removeFromSuperview];
                 }]; 
 //do not remove

ARCは使っていません

4

2 に答える 2

0

null が言ったように、 に頼ることはできませんretainCount。ARC を使用していると仮定すると、コードは実際には次のようにコンパイルされます。

FinalAlgView *dv = [[FinalAlgView alloc] init]; // Starts with retainCount of 1
self.drawView = dv; // Increments the retainCount

 NSLog(@"the retain count 1 of drawView is %d", [self.drawView retainCount]);
 //the retain count 1 of drawView is 2

...
// do not remove
...
[dv release];

ARC を使用していない場合は、コードの最初の行を次のように変更する必要があります。

self.drawView =[[[FinalAlgView alloc]init]autorelease];

実行ループのretainCount最後で自動解放プールが空になるまで、 は 2 から開始されます。

于 2013-10-02T08:08:05.210 に答える