【问题标题】:iphone combined tab bar and navigationControlleriphone组合标签栏和navigationController
【发布时间】:2011-08-20 04:07:20
【问题描述】:

我正在使用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 的正确方法是什么?

非常感谢!

【问题讨论】:

    标签: ios uitabbar navbar


    【解决方案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];
    

    【讨论】:

    • 自动发布的视图控制器作为文档示例进行管理;我需要修复的是 [tabBarController setViewControllers:tabs animated:NO];文档中没有!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-09
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多