根视图控制器和推送

Posted

技术标签:

【中文标题】根视图控制器和推送【英文标题】:root view controllers and push 【发布时间】:2014-02-14 10:18:31 【问题描述】:

美好的一天!我有 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,并且不会发生推送。

我做错了什么?

【问题讨论】:

MessengerMessengerMenu是同一个控制器吗? 奇怪,看看self.rootViewController这个可以清楚情况 您的代码似乎没有错 :( @Pavel 哦,我的意思是检查 self.parentViewController 看看你从哪里导航和做什么,如果父母有 navigationController 你可以用它来推送 显示您的更多代码...of .h 文件... 【参考方案1】:

您应该使用 UIViewController 分配所有 viewController 并将它们添加到 tabbarController。

如果你想要其中一个作为导航控制器,就放一个

现在可以将 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];

【讨论】:

他要添加每个标签是一个导航控制器 没有帮助。还有别的东西。我确实尝试只做一个控制器 [self.window setRootViewController:fourthView];有用。但是当 tabBar 中有很多控制器时,它不会【参考方案2】:

尝试使用这个 alloc init 方法来创建视图控制器的 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];

【讨论】:

still null in '- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath // 导航逻辑可能会走到这里,例如: // 创建下一个视图控制器。联系人 *detailViewController = [[Contacts alloc] initWithNibName:@"Contacts" bundle:nil]; NSLog(@"%@",self.navigationController); [self.navigationController pushViewController:detailViewController 动画:NO]; '【参考方案3】:

希望它会起作用....

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];

【讨论】:

它有效,但其他视图无效。它只显示了第三个视图 根本没有出现 我确实喜欢你的帖子。 NSLog(@"%@",self.navigationController);为空 你能用你现在正在使用的东西再次编辑你的代码吗?【参考方案4】:

添加 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];

【讨论】:

以上是关于根视图控制器和推送的主要内容,如果未能解决你的问题,请参考以下文章

在导航控制器中弹出到根视图

如何控制推送通知中的按钮的作用?

ios 最佳实践 - 导航和选项卡控制器以及推送/弹出

更改 iOS 窗口的根视图控制器

如何关闭视图控制器并快速弹出到根视图控制器?

如何停止将 UIViewController 推到自身之上?