码迷,mamicode.com
首页 > 其他好文 > 详细

UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用

时间:2015-12-01 14:27:55      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

建个空的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,在前篇文章中也有说明.

UITabBarController 笔记(三) UITabBarController 配合 UINavigationController 的使用

标签:

原文地址:http://www.cnblogs.com/Free-Thinker/p/5009848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!