【问题标题】:add two navigation controller to one Tab bar Item将两个导航控制器添加到一个选项卡栏项
【发布时间】:2011-03-10 12:10:40
【问题描述】:

我希望将 2 个导航控制器附加到一个标签栏项目。

基本上,这个想法是在一个标签项上有 2 个视图,并且应该有一个导航栏来推送和弹出屏幕。 (与 iPad 中的设置应用程序相同)。

已编辑======

看起来左侧有一个 View 有自己的导航控制器,而右侧有另一个 View 有自己的导航控制器(或其他一些 UI)来实现 Push Pop 的东西。

我知道如何将 1 个导航控制器附加到一个标签栏项目。

针对 ModalView 问题编辑:- 通过实施 conmulligan 代码,一切正常。但是,如果我尝试在 lansdscape 模式下显示一些 ModalViewController,并且当我尝试关闭此 ModalView 时,FirstViewController 导航栏将变为纵向(连同其视图),而 SecondViewController NavigationBar 仍为横向(应该如此)。这只发生在设备而不是模拟器中。

下面是我展示 ModalViewController 的代码。

ModalTableViewController *modalTableViewController = [[ModalTableViewController alloc]initWithNibName:@"ModalTableViewController" bundle:[NSBundle mainBundle]]; 
UINavigationController *localNavigationViewController = [[UINavigationController alloc] initWithRootViewController:modalTableViewController];
localNavigationViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:localNavigationViewController animated:YES];
[modalTableViewController release];
[localNavigationViewController release];

为了关闭 ModalViewCntroller,我这样做如下:-

[self dismissModalViewControllerAnimated:YES]; 

等待您的回复。

【问题讨论】:

    标签: iphone cocoa ipad


    【解决方案1】:

    一种方法是创建一个包含两个导航控制器的UIViewController 子类并将其添加到UITabBarController。以下是在UIViewController-viewDidLoad 方法中创建和布局导航控制器的方法:

    FirstViewController *firstViewController = [[FirstViewController alloc] init];
    UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    [firstViewController release];
    
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    [secondViewController release];
    
    firstNavigationController.view.frame = CGRectMake(0.f, 0.f, 320.f, self.view.frame.size.height);
    firstNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    
    secondNavigationController.view.frame = CGRectMake(321.f, 0.f, self.view.frame.size.width - 321.f, self.view.frame.size.height);
    secondNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
                                                       UIViewAutoresizingFlexibleRightMargin;
    
    [self.view addSubview:firstNavigationController.view];
    [self.view addSubview:secondNavigationController.view];
    

    这或多或少是 UISplitViewController 在后台工作的方式。

    编辑:您可能需要添加以下代码以确保其布局正确:

    - (void)viewWillAppear:(BOOL)animated { 
        [super viewWillAppear:animated];
        [firstNavigationController viewWillAppear:animated];
        [secondNavigationController viewWillAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated { 
        [super viewWillDisappear:animated];
        [firstNavigationController viewWillDisappear:animated];
        [secondNavigationController viewWillDisappear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated { 
        [super viewDidAppear:animated];
        [firstNavigationController viewDidAppear:animated];
        [secondNavigationController viewDidAppear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated { 
        [super viewDidDisappear:animated];
        [firstNavigationController viewDidDisappear:animated];
        [secondNavigationController viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    

    【讨论】:

    • -感谢您的回复。如果我尝试以横向模式启动应用程序,则从您的代码中我无法同时看到这两个视图。我只能看到一个视图,即 secondViewController。你能帮我解决这个问题吗?
    • 嗨@Ekra,这很奇怪,它对我有用。尝试将上面的额外代码添加到您的 UIViewController 子类中。
    • -感谢您的回复。我在显示上面已经解释过的 ModalView 时遇到了一些问题。请看一下。等待您的回复。
    猜你喜欢
    • 2012-12-08
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多