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

时间:2010-12-07 16:12:16

标签: iphone cocoa ios ios4

我正在尝试为iPhone启用双tabBar应用程序,并使用以下代码为基类视图控制器在视图控制器中添加多个导航控制器(请参阅下面的代码)。但问题是:尽管先前已初始化,但未向self.view添加任何子视图。有什么想法吗?

- (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];
    }
}

子类中的代码:

- (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;
}

1 个答案:

答案 0 :(得分:0)

我明白了。我在添加的视图控制器中有以下内容......

- (void)loadView
{

}

,这当然意味着根本不会加载超类。笨。否则,这种方法效果很好。 :)

相关问题