gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:47:50 24 4
gpt4 key购买 nike

我正在尝试为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com