【IOS学习】视图切换,纯代码实现 UITabBarController,NavigationController

UITabBarController UINavigationController 的使用

    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    tabBarController.delegate = self;
    
    FirstVC *firstVC = [[FirstVC alloc]init];
    UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:firstVC];
    firstNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:0];
    
    SecondVC *secondVC = [[SecondVC alloc]init];
    UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:secondVC];
    secondNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1];

    tabBarController.viewControllers = [NSArray arrayWithObjects:firstNav,secondNav, nil];
    
    // 重要:一定不能让 tabBarController 释放掉,否则会引起崩溃
    self.topTabBarController = tabBarController;
    [self.view addSubview:tabBarController.view];
    self.view.backgroundColor = [UIColor whiteColor];

pop的使用

-(void)next
{
    FirstVC *first = [[FirstVC alloc]init];
    first.currentIndex = self.currentIndex +1;
    [self.navigationController pushViewController:first animated:YES];
}

模态展示VC的方法

-(void)next
{
    SecondVC *second = [[SecondVC alloc]init];
    second.currentIndex = self.currentIndex +1;
    [self presentViewController:second animated:YES completion:nil];
}

详细例子参照:https://github.com/caigee/iosdev_sample下的
TabedNavVC

你可能感兴趣的:(ios)