zoukankan      html  css  js  c++  java
  • UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用

    建个空的iOS工程

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        //
        MyFirstViewController *viewCtrl1 = [[MyFirstViewController alloc] init];
        viewCtrl1.title = @"viewctrl1";
        viewCtrl1.view.backgroundColor = [UIColor blueColor];
        UINavigationController *navgCtrl1 = [[UINavigationController alloc] initWithRootViewController: viewCtrl1];
        //
        UIViewController *viewCtrl2 = [[UIViewController alloc] init];
        viewCtrl2.title = @"viewctrl2";
        viewCtrl2.view.backgroundColor = [UIColor redColor];
        UINavigationController *navgCtrl2 = [[UINavigationController alloc] initWithRootViewController: viewCtrl2];
        //
        UIViewController *viewCtrl3 = [[UIViewController alloc] init];
        viewCtrl3.title = @"viewctrl3";
        viewCtrl3.view.backgroundColor = [UIColor yellowColor];
        UINavigationController *navgCtrl3 = [[UINavigationController alloc] initWithRootViewController: viewCtrl3];
        //
        UITabBarController *tabBarCtrl = [[UITabBarController alloc] init];
        tabBarCtrl.view.frame = self.window.frame;
        tabBarCtrl.viewControllers = [NSArray arrayWithObjects: navgCtrl1 ,navgCtrl2, navgCtrl3 ,nil];
        
        self.window.rootViewController = tabBarCtrl;
        
        return YES;
    }

    创建自定义 MyFirstViewController (继承自UIViewController)

     
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self action:@selector(Target:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"Target" forState:UIControlStateNormal];
        button.frame = CGRectMake(100, 200, 120, 44);
        [self.view addSubview:button];
    }
    -(void)Target:(id)sender
    {
        ChangeViewController *changViewCtrl = [[ChangeViewController alloc] init];
        
        [self.navigationController pushViewController:changViewCtrl  animated:YES];
    }
     

    自定义ViewController ChangeViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.1 blue:0.9 alpha:1.0];
    }

    小结: 用UINavigationController 配合 UITabBarController,很多app都用到这样的交互,其关键点在于,给每个视图控制器(ViewController)添加导航控制器(UINavigationController).

    UINavigationController *navgCtrl1 = [[UINavigationController alloc] initWithRootViewController: viewCtrl1];

    把导航控制器打包进TabBarController

    tabBarCtrl.viewControllers = [NSArray arrayWithObjects: navgCtrl1 ,navgCtrl2, navgCtrl3 ,nil];

    把tabBarController设为应用程序rootViewController.

    self.window.rootViewController = tabBarCtrl;

    这样的话,导航控制器的后续操作可以参考自己以前写的关于 UINavigationController 的用法。以及自定义TabBarItem,在前篇文章中也有说明.

  • 相关阅读:
    mount 和 umount 命令
    mmap函数使用
    Linux系统下查看目录大小
    守护进程的创建方法和步骤
    linux中的dup()系统调用
    uboot烧写命令--yaffs、jiffs和ubifs
    对volatile关键字的理解
    linux下如何挂接(mount)光盘镜像文件、移动硬盘、U盘、Windows网络共享和NFS网络共享
    常用NFS mount选项介绍
    mount nfs 经常出错信息总结(转)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5009848.html
Copyright © 2011-2022 走看看