UIViewControllerサブビューを追加しようとしていますが、ボタンをクリックして閉じます。私の現在のコードはその仕事をしますが、それがリークするのか、それとも問題を引き起こすのかはわかりません。
最初にサブビューを追加します
-(IBAction)openNewView:(id)sender{
// start animation
[UIView beginAnimations:@"CurlUp" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// add the view
newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:newVC.view];
[UIView commitAnimations];
}
次に、newViewController.mにそれを削除する関数があります
-(IBAction)closeNewView:(id)sender{
// start animation
[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
// close dialog
[self.view removeFromSuperview];
[UIView commitAnimations];
[self.view release];
}
私が言ったように、これは機能しますが、コードを分析すると、次のようになります。
X行に割り当てられ、「newViewController」に格納されたオブジェクトの潜在的なリーク:
newViewController* newVC = [[newViewController alloc] initWithNibName:@"newViewController" bundle:[NSBundle mainBundle]];
と
呼び出し元がこの時点で所有していないオブジェクトの参照カウントの誤ったデクリメント[self.view release];
削除時autorelease
にviewControllerが[self.view release]
クラッシュするのではなく(追加後にビューを解放した場合も)、次のようになります。-[FirstViewController PerformSelector:withObject:withObject:]:割り当て解除されたインスタンス0xd21c7e0に送信されたメッセージ
[newVC release]
いずれかのviewControllersを呼び出すと、dealloc
ビルドに失敗します。
うまくいけば、私はかなり明白な質問をしていませんが、viewControllersを追加および削除する正しい方法は何ですか?