简体   繁体   中英

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). But the problem is: No subviews are added to self.view, despite them being initialized earlier. 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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM