![](/img/trans.png)
[英]How to install a NavigationController as the root view in a tab bar?
[英]iphone combined tab bar and navigationController
我使用apple文档以编程方式实现了组合的标签栏和导航,
调用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的正确方法是什么?
非常感谢!
为什么所有视图控制器都自动发布? 您可能应该保留它们并仅在完成使用后才释放它们。
至于您的结构,我发现为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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.