简体   繁体   English

在视图控制器中添加多个导航控制器?

[英]Adding multiple navigation controllers inside a view controller?

I'm trying to acheive a dual-tabBar app for the iPhone and using the following code for a base class view controller to add several navigation controllers inside the view controller (see code below). 我正在尝试实现适用于iPhone的dual-tabBar应用程序,并将以下代码用于基类视图控制器,以在视图控制器内部添加多个导航控制器(请参见下面的代码)。 But the problem is: No subviews are added to self.view, despite them being initialized earlier. 但是问题是:尽管子视图已经初始化过,但没有将子视图添加到self.view中。 Any ideas? 有任何想法吗?

- (IBAction)ViewButtonPressed:(id)sender
{
    UIButton *b = (UIButton *)sender;
    int index = b.tag - 1000;
    [self SelectNavigationController:index];
}

- (void)SelectNavigationController:(int)index
{
    // Set index to top-most view ->
    UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:index];
    [self.view bringSubviewToFront:nc.view];
}

#pragma mark -
#pragma mark display

- (void)Display
{
    CGRect frame = CGRectMake(0, 44, 320, 367);

    // Create buttons above frame and show navigation controller inside frame ->

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.origin.y)];

    for (int i=0; i<[navigationControllers count]; ++i)
    {
        UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:i];
        UIViewController *vc = [nc.viewControllers objectAtIndex:0];
        NSString *titel = vc.navigationItem.title;

        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        [b setBackgroundColor:[UIColor lightGrayColor]]; // TODO: Replace with image <-
        [b setTitle:titel forState:UIControlStateNormal];
        b.tag = i + 1000;
        [b setFrame:CGRectMake(i * frame.size.width / 3, 0, frame.size.width / 3, frame.origin.y - 1)];
        [v addSubview:b];
    }

    for (int j=0; j<[navigationControllers count]; ++j)
    {
        UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:j];
        [nc.navigationBar addSubview:v];
        [self.view addSubview:nc.view]; // Add view to view <-
        nc.view.frame = frame;
    }

    [v release];

    if (VIEW_DEBUG)
        NSLog(@"BaseTabViewController.m: self.view.subviews: %d", [self.view.subviews count]);
}

#pragma mark -
#pragma mark addviewcontroller

- (void)AddViewControllerForNavigationController:(UIViewController *)viewController
{
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    navController.view.backgroundColor = [UIColor greenColor];
    [navigationControllers addObject:navController];
    [navController release];
}

#pragma mark -
#pragma mark init, loadView, viewDidLoad and dealloc

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        navigationControllers = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)loadView
{
    //
}

- (void)viewDidLoad
{
    if (!viewDidLoadAlready)
    {
        [self Display];
        viewDidLoadAlready = YES;
        [super viewDidLoad];
    }
}

And the code in the subclass: 子类中的代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        PistKartaViewController *pistKarta = [[PistKartaViewController alloc] init];
        pistKarta.navigationItem.title = @"Pistkarta";
        LiftRapportViewController *liftRapport = [[LiftRapportViewController alloc] init];
        liftRapport.navigationItem.title = @"Liftrapport";
        SkipassViewController *skiPass = [[SkipassViewController alloc] init];
        skiPass.navigationItem.title = @"Skipass";

        [self AddViewControllerForNavigationController:pistKarta];
        [self AddViewControllerForNavigationController:liftRapport];
        [self AddViewControllerForNavigationController:skiPass];

        [pistKarta release];
        [liftRapport release];
        [skiPass release];
    }
    return self;
}

I figured it out. 我想到了。 I had the following in a view controller I was adding... 我在要添加的视图控制器中具有以下内容...

- (void)loadView
{

}

, which of course means that the superclass won't be loaded at all. ,这当然意味着根本不会加载超类。 Stupid. 笨。 Otherwise, this method works quite well. 否则,此方法效果很好。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用多个iOS自定义视图控制器而不使用导航控制器 - How to use multiple iOS custom view controllers without a navigation controller 如何在导航控制器中使用多个自定义视图控制器? - How to use multiple custom View Controllers with a Navigation Controller? 问题将多个视图控制器推送到导航控制器堆栈 - Problem pushing multiple view controllers onto navigation controller stack 查看多个导航控制器中的控制器通信 - view controllers communication in multiple Navigation Controllers 在没有导航控制器的情 - Switching view controllers without navigation controller 导航控制器未推入/弹出视图控制器 - Navigation Controller not Pushing/Popping View Controllers 添加UITabBarController之前如何显示登录或注册之类的导航控制器(视图控制器集) - How to display navigation controller (set of view controllers) such as login or register before adding UITabBarController 添加多个视图以查看选项卡控制器内的控制器 - adding multiple views to view controller inside a tab controller 将带有控制器的视图添加到带有控制器的视图-正确的方法? - Adding views with controllers to a view with controller - proper way? 在视图控制器-&gt;标签栏控制器-&gt;导航控制器-&gt;视图控制器的层次结构内旋转视图控制器 - Rotating view controllers within a hierarchy of View Controller -> Tab Bar Controller -> Navigation Controller -> View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM