【问题标题】:IOS pushing viewcontroller from navigationcontroller in a tabbarcontroller but does not showIOS在tabbarcontroller中从navigationcontroller推送viewcontroller但不显示
【发布时间】:2013-03-20 05:45:50
【问题描述】:

我遇到了一个很奇怪的问题。

这是我的应用委托中的代码结构:

self.accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil];
self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
self.exploreViewController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil];
self.activityViewController = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
self.homeNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
self.accountNavigationController = [[UINavigationController alloc] initWithRootViewController:self.accountViewController];
self.activityNavigationController = [[UINavigationController alloc] initWithRootViewController:self.activityViewController];
self.exploreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.exploreViewController];

self.tabBarController = [[MyOwnTabBarController alloc] init];
[self.tabBarController setDelegate:self];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:self.homeNavigationController, self.exploreNavigationController,self.activityNavigationController,self.accountNavigationController,nil]];
[self.tabBarController setSelectedIndex:0];
[self.navController setViewControllers:[NSArray arrayWithObjects:self.welcomeViewController, self.tabBarController, nil] animated:NO];

对于那个 self.navController,我这样定义

self.navController = [[UINavigationController alloc] initWithRootViewController:self.welcomeViewController];
self.window.rootViewController = self.navController;

所以在呈现 tabbarcontroller 之后,我将在 homeviewcontroller 那里我有一个 collectionview。它成功显示多个单元格。当我点击单元格时,

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:vc animated:YES];
}

我看到导航栏被“vc”中的新标题改变了,导航栏的后退按钮也出现了。但是,视图没有改变,我仍然可以访问 collectionview。

但是,如果我按下 tabbarcontroller 上的其他选项卡,例如 accountviewcontroller,然后再次按回 homeviewcontroller 的旧选项卡,视图现在将显示。这是一个非常奇怪的行为,我不知道它为什么会发生。

我还检查了在每个视图控制器中,self.navigationcontroller 不是 nil

【问题讨论】:

  • 您需要更彻底地描述您的应用程序结构。您是否在情节提要中制作了控制器?在代码中?
  • 大约一周前有一个非常相似的问题,但从未得到回答。如果不完整描述您的设置,您将不会获得任何有用的帮助。
  • 好的,用代码结构更新
  • 你在哪里以及如何创建你正在推送的控制器,vc?
  • 我无法从您发布的内容中判断为什么这不起作用。您对 MyOwnTabBarController 中的 UITabBarController 做了哪些更改和/或添加?我认为一般来说,将标签栏控制器嵌入导航控制器并不是一个好主意(我在 IB 中注意到,嵌入导航控制器的选项对于标签栏控制器是灰色的)。我认为最好只从第一个选项卡中显示欢迎控制器。

标签: iphone ios navigationcontroller tabbarcontroller


【解决方案1】:

发现问题出在 tabbarcontroller 的子类上

- (void)viewWillAppear:(BOOL)animated

不见了

[super viewWillAppear:animated];

【讨论】:

    【解决方案2】:

    据我了解。只需检查您是否已将导航控制器添加到 tabBar。在下面检查它。如果没有,请告诉我整个场景。

    tabBarControllerObj=[[UITabBarController alloc]init];
        NSArray *arrayObj=[[NSArray alloc]initWithObjects:navForView1,navForView2,navForView3,navForView4,navForView5, nil];
    
        [tabBarControllerObj setSelectedIndex:0];
        tabBarControllerObj.tabBar.hidden = YES;
        [tabBarControllerObj setViewControllers:arrayObj];
        [self.window addSubview:tabBarControllerObj.view];
    

    【讨论】:

    • 是的,我确信导航控制器已添加到标签栏。访问 self.navigationcontroller 不是 nil
    • 是的,但是您必须将 navigationController 添加到 tabBar。你有没有添加它们。
    • 是的,我添加了它。如果我不添加它,我什至无法看到视图。
    【解决方案3】:
    self.accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil];
    self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    self.exploreViewController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil];
    self.activityViewController = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil];
    self.homeNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
    self.accountNavigationController = [[UINavigationController alloc] initWithRootViewController:self.accountViewController];
    self.activityNavigationController = [[UINavigationController alloc] initWithRootViewController:self.activityViewController];
    self.exploreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.exploreViewController];
    if (tabBarControllerObj!=nil) {
    tabBarControllerObj=nil;
    }
    tabBarControllerObj=[[UITabBarController alloc]init];
    NSArray *arrayObj=[[NSArray alloc]initWithObjects:self.homeNavigationController,self.accountNavigationController,self.activityNavigationController,self.exploreNavigationController, nil];
    [tabBarControllerObj setSelectedIndex:0];
    tabBarControllerObj.tabBar.hidden = YES;
    [tabBarControllerObj setViewControllers:arrayObj];
    [self.window addSubview:tabBarControllerObj.view];
    

    【讨论】:

    • 只需通过删除您的代码来添加此代码。删除您添加的最后一个导航控制器。
    • 问题依然存在。奇怪的是,如果我按下另一个选项卡,然后返回,现在一切正常。我只是不知道为什么在另一个选项卡上的选项卡可以解决这个问题。
    • 它的工作代码..只需制作一个演示并尝试此代码,您将找到解决方案..
    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2016-03-10
    • 2013-11-21
    • 1970-01-01
    • 2016-11-14
    • 2014-10-30
    • 1970-01-01
    • 2011-05-08
    相关资源
    最近更新 更多