iphone - 在 View Controller 中添加多个导航 Controller ?

标签 iphone cocoa ios ios4

我正在尝试为 iPhone 实现双选项卡栏应用程序,并使用以下代码为基类 View Controller 添加 View Controller 内的多个导航 Controller (参见下面的代码)。但问题是:没有 subview 被添加到 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;
}

最佳答案

我想通了。我在添加的 View Controller 中有以下内容......

- (void)loadView
{

}

,这当然意味着根本不会加载父类(super class)。愚蠢的。否则,此方法效果很好。 :)

关于iphone - 在 View Controller 中添加多个导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4378823/

相关文章:

cocoa - 使用 Cocoa (OS X) 进行内存中 mime 类型检测?

objective-c - NSMutableArray 的子类化

ios - 删除时 CoreData 崩溃等等

iphone - UITableView - 如何在用户滚动时保持表行固定

iphone - 适用于 iOS 的 Paypal SDK : Restore purchased items

iphone - 将 UIImage 保存到照片库而不是相机胶卷

cocoa - 将控件添加到 NSTextView 并将它们绑定(bind)到(范围)字符

iphone - 4 英寸视网膜 iPhone 模拟器中页眉和页脚的剩余空间

ios - 在 Objective-C 中创建方波、锯齿波和三角波

iphone - 如何使应用程序中收集的信息在 iOS/iPhone 中持久化?