-1

在我的 iPad 应用程序中,我有一个 UINavigationController 和多个 viewControllers 作为选项卡。我不使用uitabbarcontroller,因为我想要某些自定义外观,因此一直在加载不同的控制器,这些控制器是单个uiateveviewController的子类,该子类封装了整个tableView和单元格的逻辑,并在图标按钮上用作标签,在tabs上用作标签,该单元格是在the the the the the the the the the the the the the the table contaps的逻辑。屏幕底部。

看看我已经实现的设计,我真的不需要 navigationController,因为我不需要推送/弹出视图 [我现在正在做的] 并且希望一次只有一个 viewController。

我现在所做的是:

  1. 在我的 appDelegate 的 didFinishLaunchingWithOptions 方法中,我将导航控制器分配为:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    UINavigationController *navigationVC = [[UINavigationController alloc]init];
    navigationVC.navigationBarHidden = YES;
    navigationVC.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController = navigationVC;
    [window addSubview: [self.navigationController view]];
    [navigationVC release];
    
    //set orientation as portrait
    self.currentOrientationType = PORTRAIT;
    
    //start with launch screen view controller
    [self setViewController:LAUNCH param:nil];
    
         return YES;
    }
    

在我的方法 setViewController:param: 中,根据传递给它的 ID,它正在加载适当的 viewController,如下所示:

- (void)setViewController:(NSString *)ID param:(NSString *)param {

UIViewController *viewController;

     if(ID == HOME) {
        viewController = [[HomeScreenViewController alloc]initWithNibName:@"HomeScreenViewController" bundle:nil];
     }

    else if(ID == ...){
    }

    ...

   //push the specified view controller
[self setTransitionType:nil];

[[self navigationController] initWithRootViewController:viewController];
[viewController release];
  }
}

发生的事情是我的任何 viewControllers 因此被初始化为 rootViewControllers 没有被释放。似乎每次以这种方式为每个新的 viewController 初始化 navigationController 都是错误的,因为它保留了对其根视图控制器的引用,并且我一次又一次地初始化它,而不关心它保留在早期 viewController 上的引用计数。

什么是更好的方法,因为我在任何时候都只想要一个 viewController?

4

1 回答 1

1

根据 Apple 文档,initWithRootViewController 创建了一个新的导航控制器实例。每次调用 setViewController 方法时,您都在创建一个新的导航控制器实例。相反,您需要使用 initWithRootViewController 创建一个,然后使用 PushViewController 方法创建您想要成为活动视图的视图。

于 2011-07-08T09:40:27.597 回答