iphone结合标签栏和navigationController

时间:2011-08-20 04:07:21

标签: ios uitabbar navbar

使用apple documentation

以编程方式实现组合标签栏和导航

调用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的正确方法?

非常感谢!

1 个答案:

答案 0 :(得分:1)

为什么所有的viewcontrollers都会自动发布?你可能应该保留它们并且只有在你完成它们时才释放它们。

至于你的结构,我发现为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];