根视图控制器和推送

时间:2014-02-14 10:18:31

标签: ios uitableview uinavigationcontroller pushviewcontroller

美好的一天!我有5个视图控制器。其中一个我想从UITableView单元格推送如下:

Contacts *detailViewController = [[Contacts alloc] initWithNibName:@"Contacts" bundle:nil];

NSLog(@"%@",self.navigationController);

[self.navigationController pushViewController:detailViewController animated:NO];
在appDelegate中我确实设法添加了这样的根视图控制器:

mainView = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] initWithNibName: @"MainViewController" bundle: nil]];

secondView = [[UINavigationController alloc] initWithRootViewController:[[SearchController alloc] initWithNibName: @"SearchController" bundle: nil]];

thirdView  = [[UINavigationController alloc] initWithRootViewController:[[CitySearch alloc] initWithNibName: @"CitySearch" bundle: nil]];

forthView = [[UINavigationController alloc] initWithRootViewController:[[MessengerMenu alloc] initWithNibName:@"MessengerMenu" bundle:nil]];

fifthView = [[UINavigationController alloc] initWithRootViewController:[[Contacts alloc] initWithNibName:@"Contacts" bundle:nil]];

tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: mainView ,secondView, thirdView, forthView, fifthView, nil];

[self.window setRootViewController:tabBarController];

但Messenger viewController中的self.navigationController仍然为null并且不会发生推送。

我做错了什么?

4 个答案:

答案 0 :(得分:0)

你应该使用UIViewController创建alloc all viewController并将它们添加到tabbarController。

如果,你想要其中一个作为navigationController,只需把它放在

现在可以将NavigationController的根控制器设置为tabBarController。

mainView = [[MainViewController alloc] initWithNibName: @"MainViewController" bundle: nil];

secondView = [[SearchController alloc] initWithNibName: @"SearchController" bundle: nil];

thirdView  = [CitySearch alloc] initWithNibName: @"CitySearch" bundle: nil];

forthView = [[MessengerMenu alloc] initWithNibName:@"MessengerMenu" bundle:nil];

self.navigationController =[[UINavigationController alloc] initWithRootViewController:forthView];
fifthView = [[Contacts alloc] initWithNibName:@"Contacts" bundle:nil]];

tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: mainView ,secondView, thirdView, forthView, fifthView, nil];



[self.window setRootViewController:tabBarController];

答案 1 :(得分:0)

尝试使用此alloc init方法创建viewcontrollers的NSArray,

tabBarController.viewControllers = [NSArray alloc] initWithObjects: mainView ,secondView, thirdView, forthView, fifthView, nil];

或者你先创建UITabbarViewController,将其设置为rootVC:

tabBarController = [[UITabBarController alloc] init];
[self.window setRootViewController:tabBarController];

然后:

mainView = [[UINavigationController alloc] initWithRootViewController: 
[[MainViewController alloc] initWithNibName: @"MainViewController" bundle: nil]];
 secondView =[[SearchController alloc] initWithNibName: @"SearchController" bundle: nil];
 thirdView  = [[CitySearch  alloc] initWithNibName: @"CitySearch" bundle: nil];
 forthView = [[MessengerMenu alloc] initWithNibName:@"MessengerMenu" bundle:nil];
fifthView = [[Contacts alloc] initWithNibName:@"Contacts" bundle:nil];

tabBarController.viewControllers = [NSArray arrayWithObjects: mainView ,secondView, thirdView, forthView, fifthView, nil];

答案 2 :(得分:0)

希望它会起作用....

mainView = [[MainViewController alloc] initWithNibName: @"MainViewController" bundle: nil];

secondView = [[SearchController alloc] initWithNibName: @"SearchController" bundle: nil];

thirdView  = [CitySearch alloc] initWithNibName: @"CitySearch" bundle: nil];

forthView = [[MessengerMenu alloc] initWithNibName:@"MessengerMenu" bundle:nil];


UINavigationController *navmain = [[UINavigationController alloc]initWithRootViewController:mainView];

UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:secondView];

UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:thirdView];

UINavigationController *navFourth = [[UINavigationController alloc]initWithRootViewController:forthView];
    //Second, third, fourth...

    NSArray *array = [[NSArray alloc]initWithObjects:navmain, navSecond, navThird, navFourth, nil];
        [tabBarController setViewControllers:array];

//and set selectedIndex
    [tabBarController setSelectedindex:2];

答案 3 :(得分:0)

添加viewController可能有问题吗?我按下按钮就像子视图一样添加到主视图中,如下所示:

- (IBAction)buttonMessagePressed:(UIButton *)sender {
    messController = [[MessengerMenu alloc] initWithNibName:@"MessengerMenu" bundle:nil];
    [messController.view setFrame:CGRectMake(0, 50, 300, 200)];

    for (UIView *theView in self.currenWeatherView.subviews) {
        [theView removeFromSuperview];
    }

    [self.currenWeatherView addSubview: messController.view];
}