2010-12-30 15 views
1

次のコードが100%正しいかどうか教えてください。 Expecially deallocセクション- (void)dealloc質問

FirstViewController.h

#import <UIKit/UIKit.h> 
#import "SecondViewController.h" 

@class SecondViewController 

@interface FirstViewController : UIViewController 
{ 
    SecondViewController *SecondController; 
} 

- (IBAction)SwitchView; 

@property (nonatomic, retain) IBOutlet SecondViewController *SecondController; 

@end 

FirstViewController.m

#import "FirstViewController.h" 

@implementation FirstViewController 

@synthesize SecondController; 

- (IBAction)SwitchView 
{  
    SecondController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    SecondController.modalTransitionStyle = UIModalPresentationFullScreen; 
    [self presentModalViewController:SecondController animated:YES]; 
    [SecondController release]; 
} 

/// OTHER CODE HERE /// 

- (void)dealloc 
{ 
    [SecondController release]; 
    [super dealloc]; 
} 

@end 

ありがとう!

+0

SecondControllerインスタンスを却下する責任があるのは1つの質問ですか? –

+0

あなたは本当にインスタンス変数** SecondController **を呼びたくはありません。それを 'secondController'と呼んでください。私はコンパイラがそのコードをコンパイルするのにも驚いています。 – bbum

+0

@bbumなぜ地球上でコンパイラが変数の大文字/小文字をチェックするのか? – mvds

答えて

8

いいえ正しくありません。 releaseメッセージをdeallocのポインタに送信していますが、ポインターがSecondControllerを指し示していても指していなくてもかまいません。これにより、いくつかの非常に奇妙なバグ、通常ランダムなオブジェクトがリリースされる可能性があります。

あなたのクラスはSecondControllerを保持していないので、最初にdeallocにリリースしてはいけません。ので、それを作る、正しい方法を主張し、所有権を解放するために

- (IBAction)SwitchView 
{  
    self.SecondController = [[[SecondViewController alloc] 
        initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 
    self.SecondController.modalTransitionStyle = UIModalPresentationFullScreen; 
    [self presentModalViewController:self.SecondController animated:YES]; 
} 

/// OTHER CODE HERE /// 

- (void)dealloc 
{ 
    self.SecondController = nil; 
    [super dealloc]; 
} 

これもSwitchViewdeallocの間で起こって任意の他のものからあなたを守ります。 (限り、その原料は、ルールに従うと、プロパティを変更するself.SecondController = ...を使用して)SwitchViewalloc/autorelease

シーケンスは、あなたのルーチンはルーチン(と少し超えた)の長さのために所有権を保持していることになります。 self.SecondController =部分は、(nonatomic,retain)と宣言したので、クラスにSecondControllerオブジェクトが保持されていることを確認します。

4

プロパティセッターを使用してSecondControllerを割り当てる必要があります。

私はSwitchViewでそれを示し、その後、唯一alloc/initあなたを示唆して一度コントローラを表示するには、その:

#import "FirstViewController.h" 

@implementation FirstViewController 

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
    if((self = [super initWithNibName:nibName bundle:nibBundle])) { 
     self.SecondController = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease]; 
     SecondController.modalTransitionStyle = UIModalPresentationFullScreen; 
    } 
    return self; 
} 

- (IBAction)SwitchView 
{  
    [self presentModalViewController:SecondController animated:YES]; 
} 

/// OTHER CODE HERE /// 

- (void)dealloc 
{ 
    [SecondController release]; 
    [super dealloc]; 
} 

@end 

この方法でそれを作成する-SwitchViewが呼び出されるたびに対立するものとして、あなただけ実際に、一度そのSecondControllerビューコントローラを作成。

+0

WOW速くてきれいな答え、ありがとう! :D – Aluminum

+0

このコントローラーがロードされたときに必ずsecondviewcontrollerをインスタンス化する必要はありません。これを多く行うと、アプリの起動が遅くなります。そして、もしあなたがこの道を行くなら、多分ペン先からすべてのものを初期化することを検討してください(はい、それはFirstViewとSecondViewControllerの両方です - UIViewControllerをFirstViewController nibに追加し、Class名をSecondViewControllerに変更するだけです) – mvds