更改视图控制器

时间:2012-08-03 15:41:38

标签: iphone objective-c ios

我从其他人那里得到了一个项目。问题是代码具有AppDelegate内部的setViewController方法以在视图之间进行更改。问题是这段代码似乎泄漏了内存BIG TIME。我一直试图修复代码,但我似乎无法做到这一点。你认为这是最好的解决方案?修改此代码或一起使用导航控制器?

// The Code to change the views
- (void)setViewController:(UIViewController*)viewController
{
    [UIView transitionWithView:_window duration:0.5 options:UIViewAnimationOptionTransitionNone animations:^(void) 
     {
         BOOL oldState = [UIView areAnimationsEnabled];
         [UIView setAnimationsEnabled:NO];
         _window.rootViewController = viewController;
         [UIView setAnimationsEnabled:oldState];
     } 
                    completion:nil];

}

// The calls to the setViewController
-(void) gotoHowToUseView{
    [self setViewController:[[[HowToVC alloc] initWithNibName:@"HowToVC" bundle:nil] autorelease]];
}
-(void) gotoHowToCredits{
    [self setViewController:[[[CreditsVC alloc] initWithNibName:@"CreditsVC" bundle:nil] autorelease]];
}

1 个答案:

答案 0 :(得分:0)

我认为最好使用UINavigation Controller。

现在,您的代码解决方案是从两个函数中删除自动释放。将这两行添加到根视图控制器功能

  _window.rootViewController = nil;  // add this line
  _window.rootViewController = viewController; 
  [viewController release]; // add this line