0

我正在尝试通过在 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];
4

4 回答 4

3

您不能将它添加到当前视图控制器您需要做的不是呈现 ViewController 而是将此视图控制器添加到导航视图控制器并呈现

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

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

NSArray arrayWithObjects:viewController1, 
                                 SecondViewNavCont, 
                                 viewController3, 
                                 nil];
于 2012-06-18T11:54:22.597 回答
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;
}

欢迎!

于 2012-06-18T11:59:13.857 回答
1

您需要为navController对象设置 RootViewController。然后您可以进一步使用 pushViewController 方法推送 UIViewcontroller 对象。

于 2012-06-18T11:53:41.257 回答
1

您需要为该导航控制器添加一个视图。添加的导航控制器没有视图,因此它覆盖在 UIViewController 您需要添加

 [[UINavigationController alloc] initWithNibName:@"ViewControllerName" bundle:nil];
于 2012-06-18T11:54:40.037 回答