3

I dynamically add a subview to another uiview;

in the subview ,click a button to go back by the following method:

[self removeFromSuperView ];

but after manny times added subview and removed it,my app could crash ,the log said it was killed .

I found that calling [self removeFromSuperView] didn't release self. So what's the best methods to releas it?

4

3 に答える 3

4

作成時に UIView を保持している (または配列に追加している) 場合、保持カウントが増加します。例えば:

// Retain count is 1
UIView *myView = [[UIView alloc] initWithFrame:myFrame];

// Retain count is 2
[myParentView addSubview:myView];

// Retain count is 1 again
[myView removeFromSuperView];

上記の例autoreleaseでは、サブビューとしてすぐに追加された場合はビューを、iVar の場合は dealloc で解放できます。

編集:(あなたの見解が保持される可能性のある他の理由)

// Retain count +1
[myArray addObject:myView];

// Retained in the setter created by the @synthesize directive
@property(nonatomic, retain) UIView *myView;

プロパティが保持されているとドキュメントに記載されているその他のすべて。

また、VC の loadView メソッドでオブジェクトを作成する際にも注意が必要です。それらを確実に解放する場合は、loadView が呼び出されたときにオブジェクトが再度作成されるためです。これは、VC のビューがアンロードされてから再ロードされた場合に発生します。

于 2012-07-13T06:17:47.750 に答える
2

最初にリリースする必要があります。「alloc」の対応は「release」であり、「addSubview」の対応は「removeFromSuperView」です。これらのバランスを保ちます。

ビューを追加:

UIView *myView = [[UIView alloc] initWithFrame:myFrame];
[myParentView addSubview:myView];
[myView release];

ビューを削除します (ビューは removeFromSuperView の後にメモリ内でクリアされます):

[myView removeFromSuperView];
于 2012-07-13T08:10:28.733 に答える
0

保持されたビューをサブビューとして追加しているようです。その親ビューは再びそれを保持します。そのため、セル[self removeFromSuperView];に入れると、superView からリリース メッセージが表示されますが、作成者がリリースする必要があります。

于 2012-07-13T06:06:29.370 に答える