iOS如何跳转到一个有UINavigationController的界面(即一开始没有UINavigationController)

前言

在一开始学习UINavigationController与UITabBarController的时候,我们使用的是修改AppDelegate.m文件,将self.window.rootViewController设为UITabBarController,将每个UINavigationController添加到UITabBarController管理数组中。
可在制作share项目时,多了一个登录界面,登陆之后才是跳转到首页,也就是一开始没有没有UINavigationController与UITabBarController,为了解决这个问题,我在网上搜了好久,看到了好几篇一模一样的文章,开的头一句话都是在AppDelegate.m里XXXX,把我烦的。现在解决了,写篇文章科普下,另外搜出来十几篇一模一样的的文章是真的难受。

示例代码

// 我要实现的是登录后进入首页,所以这段代码在登录的文件里
//控制器创建(导航栏)
    ShareHome *shareHome = [[ShareHome alloc] init];
    shareHome.tabBarItem.image = [[UIImage imageNamed:@"radio_button1_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareHome.tabBarItem.selectedImage = [[UIImage imageNamed:@"radio_button1_pressed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ShareSearch *shareSearch = [[ShareSearch alloc] init];
    shareSearch.tabBarItem.image = [[UIImage imageNamed:@"radio_button2_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareSearch.tabBarItem.selectedImage = [[UIImage imageNamed:@"radio_button2_pressed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ShareArticle *shareArticle = [[ShareArticle alloc] init];
    shareArticle.tabBarItem.image = [[UIImage imageNamed:@"radio_button3_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareArticle.tabBarItem.selectedImage = [[UIImage imageNamed:@"radio_button3_pressed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ShareActivity *shareActivity = [[ShareActivity alloc] init];
    shareActivity.tabBarItem.image = [[UIImage imageNamed:@"radio_button4_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareActivity.tabBarItem.selectedImage = [[UIImage imageNamed:@"radio_button4_pressed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    ShareInformation *shareInformation = [[ShareInformation alloc] init];
    shareInformation.tabBarItem.image = [[UIImage imageNamed:@"radio_button5_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    shareInformation.tabBarItem.selectedImage = [[UIImage imageNamed:@"radio_button5_pressed"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    //分栏相关
    UITabBarController* tbController = [[UITabBarController alloc] init];
    tbController.tabBar.translucent = NO;
    tbController.tabBar.backgroundColor = [UIColor blackColor];

    [[UITabBar appearance]setBackgroundColor:[UIColor blackColor]];

    //导航栏相关
    UINavigationController * navHome = [[UINavigationController alloc]initWithRootViewController:shareHome];
    UINavigationController * navSearch = [[UINavigationController alloc]initWithRootViewController:shareSearch];
    UINavigationController * navArticle = [[UINavigationController alloc]initWithRootViewController:shareArticle];
    UINavigationController * navInformation = [[UINavigationController alloc]initWithRootViewController:shareInformation];
    UINavigationController * navActivity = [[UINavigationController alloc]initWithRootViewController:shareActivity];

    NSArray* arrayMY = [NSArray arrayWithObjects:navHome, navSearch, navArticle, navActivity, navInformation, nil];
    tbController.viewControllers = arrayMY;


    tbController.selectedIndex = 0;

    [self presentViewController:tbController animated:YES completion:nil];

讲解

  1. 其实这段代码现在看看无比简单,当时还是只知道依葫芦画瓢,不懂每一行代码的意思,导致卡着
  2. 我们在AppDelegate.m文件里设置根视图其实就是进去后的第一个视图,所以想实现跳转到一个有UINavigationController的界面,只要把以前放在AppDelegate.m里那一套搬过来到land中的点击事件里,然后关键的是presentViewController:tbController 切记不是UIVIewController

猜你喜欢

转载自blog.csdn.net/KevinAshen/article/details/81389821
今日推荐