`
yunlonglove
  • 浏览: 231061 次
社区版块
存档分类
最新评论

IOS学习笔记12—UITabBarController

 
阅读更多

UITabBarController是选项卡栏导航控制器,显示效果是在页面底部有多个选项卡,通过点击不同选项卡可以在不同的ViewController之间进行切换。


这种对象的层次结构至少包含6个对象:

一个UITabBarController;

两个UIViewController;

一个UITabBar;

两个UITabBarItem;


UITabBarController是选项卡栏视图控制器,UITabBar是底部两个UITabBarItem的容器,管理两个UITabBarItem,每个UITabBarItem对应一个UIViewController,然后每个UIViewController都有自己的视图和视图控制器。


UITabBarController中有一个viewControllers属性,这是一个NSArray,包含选项卡控制器的视图控制器


下面来用代码创建一个UITabBarController:

下面是工程结构:

首先创建两个带xib文件的ViewController,分别为FirstViewController和SecondViewController

然后在AppDelegate.h中声明@property (strong,nonatomic) UITabBarController *tabBarController;,并添加协议UITabBarControllerDelegate


在.m中实现如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
   
    //第一个tab的viewController
    FirstViewController *firstViewController = [[FirstViewController alloc]init];
   
    //如果在这里指定tabitem标题,则在FirstViewController中指定self.tabBarItem.title则不生效   
    firstViewController.title = @"First view";
   
    UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1];
    [firstItem setFinishedSelectedImage:[UIImage imageNamed:@"p1"] withFinishedUnselectedImage:[UIImage imageNamed:@"p1_f"]];
    firstViewController.tabBarItem = firstItem;
   
   
    SecondViewController *secondViewController = [[SecondViewController alloc]init];
   
    //构建TabBarItem
    UITabBarItem *secondItem = [[UITabBarItem alloc]initWithTitle:@"Second" image:nil tag:2];
   
    //设置选中和非选中状态下的图片
    [secondItem setFinishedSelectedImage:[UIImage imageNamed:@"p2_f"] withFinishedUnselectedImage:[UIImage imageNamed:@"p2"]];
    //右上角小图标
   [secondItem setBadgeValue:@"2"];
    //指定tabBarItem
    secondViewController.tabBarItem = secondItem;
    [secondItem release];
   
    //构建UITabBarController并指定代理为本身
    self.tabBarController = [[[UITabBarController alloc]init] autorelease];
    self.tabBarController.delegate = self;
   
    //为UITabBarController添加TabBarItem
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController, nil];
   
    [firstViewController release];
    [secondViewController release];
   
    //设置选中哪个tab
//    [self.tabBarController setSelectedIndex:0];
   
    //指定根视图
    self.window.rootViewController  = self.tabBarController;
   
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}



效果比较简单,有需要源码的可以给我留言,谢谢,另外,欢迎关注我的新浪微博:唐韧_Ryan

分享到:
评论

相关推荐

    iOS 自定义UINavigationController和UITabBarController

    在iOS应用开发中,`UINavigationController`和`UITabBarController`是两个核心的控制器,用于构建常见的用户界面结构。它们分别是导航栈和标签页切换器,但有时开发者可能需要根据应用的需求进行定制,以实现独特的...

    ios-自定义UITabBarController.zip

    在iOS开发中,`UITabBarController` 是一个非常常见的组件,用于实现底部标签栏导航。在实际项目中,为了满足个性化需求,我们往往需要对它进行定制,使其更符合应用的风格和交互设计。"ios-自定义...

    纯代码搭建iOS三级结构(UITabbarController+UINavigationController+UIViewController)

    首先,`UITabBarController`是iOS中的一个核心组件,它允许在底部展示多个选项卡,每个选项卡代表一个不同的视图控制器。用户可以通过点击选项卡在不同的视图之间切换。`UITabBarController`通过其`tabBar`属性提供...

    ios-UITabBarController.zip

    自定义UITabBarController

    ios-完全自定义UITabBarController里的标签样式.zip

    iOS Object-C 自定义封装成 和 UITabBarView一样的功能。主要体现在 UITabBar样式上,可以随意修改 继承UITabBarController 写的 管理切换控制器 主要是写 UITabBar样式是 继承UIView

    iOS应用开发中UITabBarController标签栏控制器使用进阶

    在iOS应用开发中,`UITabBarController` 是一个核心组件,用于实现底部的标签栏界面,用户可以通过点击不同的标签在多个视图控制器间切换。`UINavigationController` 则是用来管理一系列视图控制器的堆栈,提供导航...

    自定义UITabBarController

    在iOS应用开发中,`UITabBarController` 是一个非常重要的组件,它用于管理多个子控制器,每个子控制器对应TabBar上的一个标签。自定义`UITabBarController` 主要是为了满足开发者对用户界面的个性化需求,比如改变...

    IOS UITabBarController 使用示例

    在iOS应用开发中,`UITabBarController` 是一个非常重要的组件,它用于创建具有底部标签栏界面的应用。这个组件允许用户在多个视图...学习和熟练掌握 `UITabBarController` 的使用,是iOS开发过程中不可或缺的一部分。

    ios-Swift UITabBarController.zip

    在这个"ios-Swift UITabBarController.zip"项目中,我们可以学习到如何用Swift来实现`UITabBarController`的基本功能。 首先,我们需要理解`UITabBarController`的工作原理。它管理一组子视图控制器,每个子视图...

    iOS-Swift-UITabBarController-Demo:UITabBarController的布局Demo

    iOS-Swift-UITabBarController-Demo方便学习的UITabBarController应用使用了全局navigationController的应用加入了iconfonts的使用,用来制作图标有一个Transform的动画可供学习自定义了Tabbar样式Swift语言

    ios自定义UITabBarController

    在iOS应用开发中,`UITabBarController` 是一个非常重要的组件,它用于管理多个视图控制器,并在底部显示一个选项卡栏,用户可以通过点击这些选项卡在不同的视图之间切换。然而,系统的`UITabBarController` 默认...

    ios-自定义UITabBarController,完美的搭建框架,可以直接用在项目中,还有完美的UIwebView.zip

    在iOS应用开发中,UITabBarController是苹果提供的一个标准组件,用于实现底部标签栏的切换效果,方便用户在多个视图控制器间进行导航。然而,系统默认的UITabBarController有时不能满足开发者对于个性化和功能扩展...

    iOS开发UITabBarController和UICollectionView的使用Demo

    在iOS应用开发中,`UITabBarController` 和 `UICollectionView` 是两个非常重要的组件。`UITabBarController` 用于实现底部的标签栏切换功能,而 `UICollectionView` 则是一种高度可定制化的视图,用于展示网格或者...

    ios-uitabbarcontroller的封装.zip

    在iOS应用开发中,`UITabBarController` 是一个非常重要的组件,它用于管理多个`UIViewController`实例,并在底部展示一个带有多个选项卡的导航栏。`UITabBarController`的封装通常是为了实现更灵活的定制、更好的...

Global site tag (gtag.js) - Google Analytics