2010-06-10 2 views
0

Je suis juste curieux en ce qui concerne la façon correcte de créer un contrôleur de vue par programmation. Quand je compile ce code avec l'analyseur statique, je reçois une fuite (comme vous pouvez vous y attendre) de l'alloc. Devrais-je le laisser car il doit rester jusqu'à ce que l'application sorte de toute façon, ou y a-t-il un moyen plus propre?AppDelegate viewController fuite de mémoire?

- (BOOL)application:(UIApplication *)application 
     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    NSLog(@"UIApplication application:"); 
    RectViewController *myController = [[RectViewController alloc] init]; 
    [window addSubview:[myController view]]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 

acclamations Gary

Répondre

3

Dans ce cas, conserver une référence à votre contrôleur de vue comme une variable d'instance sur le AppDelegate et le libérer dans la méthode dealloc du AppDelegate.

@interface AppDelegate : NSObject { 
    // ... 
    RectViewController *myController; 
} 

// ... 
@end 


@implementation AppDelegate 

// ... 

- (BOOL)application:(UIApplication *)application 
     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    NSLog(@"UIApplication application:"); 
    myController = [[RectViewController alloc] init]; 
    [window addSubview:[myController view]]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 

- (void) dealloc { 
    // ... 
    [myController release]; 

    [super dealloc]; 
} 

// ... 

@end 
+0

Merci Will, juste ce que j'étais après. – fuzzygoat

2

Conserver une référence au contrôleur de vue en déléguer l'application (par exemple, la propriété, synthétisent et libèrent dans dealloc).

Et puis instancier comme ceci:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    NSLog(@"UIApplication application:"); 

    RectViewController *rootControllerTemp = [RectViewController new]; 
    self.rootController = rootControllerTemp; 
    [rootControllerTemp release]; 

    [window addSubview:[self.rootController view]]; 

    [window makeKeyAndVisible]; 

    return YES; 
} 
+1

en tapant la même chose! bat-moi à ça! LOL – slf

+0

Merci, très apprécié Michael. – fuzzygoat