我正在尝试通过在ViewDidLoad中添加UINavigationController来将其添加到现有的视图控制器中:
- (void)viewDidLoad
{
[super viewDidLoad];
navController = [[UINavigationController alloc]init];
[self.view addSubview:navController.view];
}
但通过这样做,它完全阻止了我的观点。它将UINavigationBar放在顶部,但视图的其余部分不响应输入。
这就是我呈现视图的方式。 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];
答案 0 :(得分:3)
您无法将其添加到当前视图控制器 你需要做的不是呈现ViewController而是将这个viewcontroller添加到导航视图控制器并呈现
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:YourPresentedViewController];
//then present the navController
[self presentModalViewController:navController animated:YES];
现在,当您出示时,请执行以下操作
NSArray arrayWithObjects:viewController1,
SecondViewNavCont,
viewController3,
nil];
答案 1 :(得分:2)
您可以尝试使用此代码将uiviewcontroller加载到uinavigationcontroller:
yourviewController *viewcontroller=[[yourviewController alloc] initWithNibName:@"yourviewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[self presentModalViewController:navController animated:YES];
(或)你想在应用程序启动时加载uinavigationcontroller尝试下面的应用程序委托类代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller.view];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
欢迎!
答案 2 :(得分:1)
您需要为navController
对象设置RootViewController。然后你可以使用pushViewController方法推送UIViewcontroller对象。
答案 3 :(得分:1)
您需要为该导航控制器添加视图。添加的导航控制器没有视图,因此它覆盖在UIViewController上 你需要添加
[[UINavigationController alloc] initWithNibName:@"ViewControllerName" bundle:nil];