![](/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.