2012-07-23 65 views
0

我想用我自己的控制器在uiviewcontrollers之間進行導航。所以就像我可以在我的意見之間進行自定義轉換。以這種方式切換UIViewController是否正確?

我做了一些工作,但我不確定它是否正確?以後會不會有與記憶有關的問題?......我不確定一切都以正確的方式被釋放。

感謝您的幫助!

這裏是我的代碼:

在我的AppDelegate,在啓動時:

self.rootVC = [[[MenuView alloc] initWithNibName:@"MenuView" bundle:nil] autorelease]; 
[self.window addSubview:self.rootVC.view]; 

在我的AppDelegate,用於切換UIViewController中的方法:

-(void) changeRootController: (NSString*) ctrlName{ 

    UIViewController *oldVC = self.curVC; 
    UIViewController *newVC; 

    if(ctrlName == @"Studio"){ 
     newVC = [[StudioView alloc] initWithNibName:@"StudioView" bundle:nil]; 
    } 
    else if(ctrlName == @"Menu"){ 
     newVC = [[MenuView alloc] initWithNibName:@"MenuView" bundle:nil]; 
    } 

    self.curVC = newVC; 

    [UIView transitionWithView:self.window 
      duration:1.0 
      options:UIViewAnimationOptionCurveEaseIn 
      animations:^{ 
       newVC.view.transform = CGAffineTransformConcat(newVC.view.transform, CGAffineTransformMakeTranslation(-1024, 0)); 
       [self.rootVC.view addSubview:newVC.view]; 
       newVC.view.transform = CGAffineTransformConcat(newVC.view.transform, CGAffineTransformMakeTranslation(1024, 0)); 
       oldVC.view.transform = CGAffineTransformConcat(oldVC.view.transform, CGAffineTransformMakeTranslation(1024, 0)); 
      } 
      completion:^(BOOL finished){ 
       if(finished){ 
        [oldVC release]; 
       } 
      } 
    ]; 
} 

我切換在我的UIViewController視圖如下:

AppDelegate *ad = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
[ad changeRootController:@"Studio"]; 
+1

爲什麼不使用ARC? – Stavash 2012-07-23 12:10:35

回答

1

newVC未發佈。

該塊應該保留newVc,以便在您轉換後您應該調用 。 (函數最後一行)

[newVC autorelease]; 

那假設self.curVC屬性是強還是保留

相關問題