0

我使用苹果文档以编程方式实现组合标签栏和导航,

调用initWithFrame时它不起作用,[黑屏];但如果留下如下代码,它适用于显示主屏幕,没有标签栏,使用标签栏时会黑屏

这里的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(  NSDictionary *)launchOptions {                  
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
StartViewController *startViewControllerView = [[[StartViewController alloc] init] autorelease]; //ojo recomendado por apple!!!
VideosViewController* VideosViewController_ = [[[VideosViewController alloc] init] autorelease];
PhotosViewController* PhotosViewController_ = [[[PhotosViewController alloc] init] autorelease];
SocialViewController* SocialViewController_ = [[[SocialViewController alloc] init] autorelease];
self.pagesNavigation = [[[UINavigationController alloc] initWithRootViewController:startViewControllerView] autorelease];
self.pagesNavigation.navigationBarHidden = NO;
NSArray* controllers = [NSArray arrayWithObjects:VideosViewController_, PhotosViewController_, SocialViewController_, startViewControllerView, nil];
self.tabBarController.viewControllers = controllers;
[self.window addSubview:startViewControllerView.view];
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
  return YES;
}

因此,如果如上所示,它可以工作,但是如果我评论 addSubview 并取消注释 initWithFrame,它就不起作用,,

  //[self.window addSubview:startViewControllerView.view];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

那么,我错过了什么?,调用 initWithFrame 的正确方法是什么?

多谢!

4

1 回答 1

1

为什么你所有的视图控制器都会自动释放?您可能应该保留它们并仅在完成它们时才释放它们。

至于您的结构,我发现为 tabbarcontroller 中的每个选项卡构建一个导航控制器,将它们添加到控制器,然后将 tabbarcontroller 添加到窗口中,就像这样......

AppDelegate.h

property (nonatomic, retain) UITabBarController *tabBarController;
property (nonatomic, retain) UINavigationController *firstNavController;
property (nonatomic, retain) UINavigationController *secondNavController;
property (nonatomic, retain) FirstViewController *firstViewController;
property (nonatomic, retain) SecondViewController *secondViewController;

AppDelegate.m

firstViewController = [[FirstViewController alloc] someInitMethod:someArg];
firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; 

secondViewController = [[SecondViewController alloc] someInitMethod:someArg];
secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; 

tabBarController = [[UITabbarController alloc] init];

NSArray *tabs = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];

[tabBarController setViewControllers:tabs animated:NO];

self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
于 2011-08-20T05:47:55.590 回答