• iOS:删除storyBoard,纯代码实现UITabBarController的视图切换功能


      storyboard是一个很强大的编写代码的辅助工具,可以帮助布局多个视图之间的联系,既直观又能减少代码量;但是,作为一个程序员,在不使用storyboard的情况下,纯代码编写是必须的技能。

    下面就用纯代码实现纯代码实现UITabBarController的视图切换功能,咱就实现三个视图之间的转换吧,代码不多,容易看的明白。

    步骤:

    1、删除storyboard故事板和UIViewController

    2、创建三个控制器类,均继承自UIViewController,分别为FirstViewController、SecondViewController、ThreeViewController

    3、为了便于区分跳转的视图,分别在上面的三个控制器类中设置它们各自视图的颜色。

    4、在AppDelegate应用程序代理类中进行这三个控制器的创建、UITabBarController的创建、window的创建。最后进行代码的整合即可。

    文件截图如下:

    演示结果如下:

           

    代码如下:

    在FirstViewController类中只设置视图颜色:

    1 - (void)viewDidLoad {
    2     [super viewDidLoad];
    3     // 设置视图颜色
    4     self.view.backgroundColor = [UIColor redColor];
    5 }

    在SecondViewController类中只设置视图颜色

    1 - (void)viewDidLoad {
    2     [super viewDidLoad];
    3      // 设置视图颜色
    4     self.view.backgroundColor = [UIColor greenColor];
    5 }

    在ThreeViewController类中只设置视图颜色

    1 - (void)viewDidLoad {
    2     [super viewDidLoad];
    3      // 设置视图颜色
    4     self.view.backgroundColor = [UIColor purpleColor];
    5 }

    在AppDelegate应用程序代理类中,代码才是重点,如下:

     1 #import "AppDelegate.h"
     2 #import "FirstViewController.h"
     3 #import "SecondViewController.h"
     4 #import "ThreeViewController.h"
     5 
     6 @interface AppDelegate ()
     7 
     8 @end
     9 
    10 @implementation AppDelegate
    11 
    12 
    13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    14     // 创建window并设置大小
    15     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    16     
    17     //创建UITabBarController
    18     UITabBarController *tabBarController = [[UITabBarController alloc]init];
    19     
    20     //创建三个控制器,并且加入tabBarController中
    21     FirstViewController *firstVC = [[FirstViewController alloc]init];
    22     //设置标签栏标题
    23     firstVC.tabBarItem.title = @"first";
    24     //设置系统自带的图标
    25     firstVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
    26     //设置badgeValue
    27     firstVC.tabBarItem.badgeValue = @"10";
    28     
    29     SecondViewController *secondVC = [[SecondViewController alloc]init];
    30     secondVC.tabBarItem.title = @"second";
    31     //设置系统自带的图标
    32     secondVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
    33     //设置badgeValue
    34     secondVC.tabBarItem.badgeValue = @"5";
    35     
    36     
    37     ThreeViewController *threeVc = [[ThreeViewController alloc]init];
    38     threeVc.tabBarItem.title = @"three";
    39     //设置系统自带的图标
    40     threeVc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2];
    41     
    42     //[tabBarController addChildViewController:firstVC];
    43     //[tabBarController addChildViewController:secondVC];
    44     //[tabBarController addChildViewController:threeVC];
    45     tabBarController.viewControllers = @[firstVC,secondVC,threeVc];
    46     
    47     //将tabBarcontroller设置为根控制器
    48     self.window.rootViewController = tabBarController;
    49     
    50     //window接受用户响应并显示
    51     [self.window makeKeyAndVisible];
    52     
    53     return YES;
    54 }
  • 相关阅读:
    ASP.NET中常用服务器端与客户端交互!
    使用WebDeployment Project改善VS2005发布网站问题 (一) 基础
    MSN改密码
    Ajax学习(一)
    Visual Studio2005 + Visual SourceSafe 2005 实现团队开发、源代码管理、版本控制
    手把手教你如何配置和编译ogre 1.7.0 + cegui 0.7.1
    What's Coming in the June 2010 DirectX SDK
    黄仁勋:相信你正在做的事。
    [转载] 翻译 西川善司著针对3D游戏爱好者的”METAL GEAR SOLID 4”图形讲座(下)
    glPushAttrib里的各个BIT都save了些什么
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4811401.html
Copyright © 2020-2023  润新知