gpt4 book ai didi

ios - 将 UINavigationController 添加到现有 UIViewController 的正确方法

转载 作者:行者123 更新时间:2023-11-28 18:13:29 28 4
gpt4 key购买 nike

我正在尝试通过在 ViewDidLoad 中添加一个 UINavigationController 到我现有的 View Controller :

- (void)viewDidLoad
{
[super viewDidLoad];

navController = [[UINavigationController alloc]init];

[self.view addSubview:navController.view];
}

但是这样一来,完全挡住了我的视线。它将 UINavigationBar 放在顶部,但 View 的其余部分不响应输入。

这就是我呈现观点的方式。 SecondViewController 是我想要的 NavController:

UITabBarController *tabController = [[UITabBarController alloc] init];

FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"CardsViewController" bundle:nil];

UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"First"
image:[UIImage imageNamed:@"img1.png"] tag:1];
[viewController1 setTabBarItem:tab1];

SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"ShoppingViewController" bundle:nil];

UINavigationController *SecondViewNavCont = [[UINavigationController alloc]initWithRootViewController:viewController2];

UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Second"
image:[UIImage imageNamed:@"img2.png"] tag:2];
[SecondViewNavCont setTabBarItem:tab2];

UIViewController *viewController3 = [[UIViewController alloc] init];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Third"
image:[UIImage imageNamed:@"img3.png"] tag:3];
[viewController3 setTabBarItem:tab3];


tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
nil];

[self.view addSubview:tabController.view];

最佳答案

你不能将它添加到当前 View Controller 您需要做的不是呈现 ViewController,而是将此 viewcontroller 添加到 navigationview Controller 并呈现它

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:YourPresentedViewController];
//then present the navController
[self presentModalViewController:navController animated:YES];

现在,当您展示它时,请执行以下操作

NSArray arrayWithObjects:viewController1, 
SecondViewNavCont,
viewController3,
nil];

关于ios - 将 UINavigationController 添加到现有 UIViewController 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11082203/

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