两种常用的app导航结构(UINavigationController+UITabBarController)

今天复习了下两种app导航(UINavigationController+UITabBarController)的架构:

1、window(或者如有登陆页面,登陆成功后presentViewController到tabController)的rootViewController为UITabBarController,UITabBarController的viewControllers设置为n个UINavigationController,每个UINavigationViewController的rootViewController为自定义VC;

2、rootViewController为UINavigationController,UINavigationController的rootViewControllers设置为一个自定义UITabBarController,UITabBarController的viewControllers设置为n个UINavigationController,每个UINavigationViewController的rootViewController为自定义VC;

显然第二种方式多了个NavigationController,别人一个demo地址https://github.com/JNTian/JTNavigationController ;

自己一直都使用第一种方式,流程图如下

a、首先在Appdelegate里添加如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UITabBarController * tabController = [[UITabBarController alloc] init];
    UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]];//UIViewController可以自定义
    nav1.tabBarItem.title = @"first";
    nav1.tabBarItem.image = nil;
//    nav1.navigationBar.backgroundColor = [UIColor redColor];//背景而已
    nav1.navigationBar.barTintColor = [UIColor redColor];
    nav1.hidesBottomBarWhenPushed = YES;
    //看来标题要在每个vc中设置self.title
//    nav1.title = @"first";
//    nav1.navigationItem.title = @"first";
    UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc]init]];
    nav2.tabBarItem.title = @"second";
    nav2.tabBarItem.image = nil;
    UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc]init]];
    nav3.tabBarItem.title = @"three";
    nav3.tabBarItem.image = nil;
    [tabController setViewControllers:@[nav1,nav2,nav3]];
    self.window.rootViewController = tabController;//[[UINavigationController alloc] init];
    //[[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    return YES;
}

b、在自定义VC类中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"first";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(didTapNextButton)];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didTapNextButton{
    ViewController* vc = [[ViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

最后附上代码地址:https://github.com/Carl-Yao/DemoForNavigation.git

posted @ 2016-02-17 16:59  yaozhx  阅读(1583)  评论(0编辑  收藏  举报