zoukankan      html  css  js  c++  java
  • iOS开发——代码生成TabBar与视图切换具体解释

            我在之前多篇博客中解说了在不使用storyboard而使用nib文件的情况下。使用代码生成导航栏并进行跳转,具体能够參考《iOS开发——界面跳转与返回及视图类型具体解释》《iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)》。

    今天我来解说下在使用nib搭建界面的情况下,用代码生成TabBar,并进行界面之间的跳转。代码演示样例已经上传至:https://github.com/chenyufeng1991/TabBarTest   。

    (1)在该演示样例中,Navigation和TabBar都会通过代码来实现。所以须要在AppDelegate中初始化设置例如以下:当中RootViewController是在后面定义的一个根视图。

    #import "AppDelegate.h"
    #import "RootViewController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
      //声明根视图;
      RootViewController *root = [[RootViewController alloc]init];
      self.window.rootViewController = root;
    
      [self.window makeKeyAndVisible];
    
      return YES;
    }
    
    @end


    (2)RootViewController定义了根视图,在这里定义了页面的Navigation和TabBar。这是我们第一个看到的视图。

    #import "RootViewController.h"
    #import "FirstViewController.h"
    #import "SecondViewController.h"
    
    @interface RootViewController ()<UITabBarControllerDelegate>
    
    //声明TabBar
    @property (nonatomic,strong)UITabBarController *tabBarController;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
      [super viewDidLoad];
    
      UITabBarController *tabBarController = [[UITabBarController alloc]init];
      tabBarController.delegate = self;
      /**
       把两个界面增加到根视图中;
       两个界面也分别要导航栏。
       */
      FirstViewController *firstVC = [[FirstViewController alloc]init];
      UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:firstVC];
      firstNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:0];
    
      SecondViewController *secondVC = [[SecondViewController alloc]init];
      UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:secondVC];
      secondNav.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
    
      //通过数组存储。
      tabBarController.viewControllers = [NSArray arrayWithObjects:firstNav,secondNav, nil];
    
      self.tabBarController = tabBarController;
      [self.view addSubview:tabBarController.view];
    }
    
    @end

    (3)TabBar的第一个Tab实现例如以下,我这里通过一个button以push方式跳到还有一个页面(也会出现导航栏和TabBar)。

    #import "FirstViewController.h"
    #import "First02ViewController.h"
    
    @interface FirstViewController ()
    
    @end
    
    @implementation FirstViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      self.title = @"1111";
    
    }
    
    - (IBAction)buttonPressed:(id)sender {
    
      //通过push跳到还有一个界面;
      First02ViewController *first02 = [[First02ViewController alloc] init];
      [self.navigationController pushViewController:first02 animated:true];
    
    }
    
    @end

    (4)在上述push到还有一个界面后,能够使用导航栏自带的“返回”button返回。也能够通过pop返回:

    #import "First02ViewController.h"
    
    @interface First02ViewController ()
    
    @end
    
    @implementation First02ViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      self.title = @"新闻";
    
    }
    
    - (IBAction)backButtonPressed:(id)sender {
    
      //通过pop返回到push过来的界面;
      [self.navigationController popViewControllerAnimated:true];
    }
    
    @end


    (5)在第二个Tab中。我通过点击button以Modal方式跳转到还有一个页面(该页面没有导航栏,没有TabBar)。

    #import "SecondViewController.h"
    #import "Second02ViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      self.title = @"2222";
    
    }
    
    - (IBAction)buttonPressed:(id)sender {
    
      //通过modal方式跳转,跳过去后的界面没有导航栏。
      Second02ViewController *second02 = [[Second02ViewController alloc] init];
      [self presentViewController:second02 animated:true completion:nil];
    
    }
    
    @end

    然后通过dismiss返回。

    #import "Second02ViewController.h"
    
    @interface Second02ViewController ()
    
    @end
    
    @implementation Second02ViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
    }
    
    - (IBAction)backButtonPressed:(id)sender {
    
      //通过dismiss返回modal过来的界面;
      [self dismissViewControllerAnimated:true completion:nil];
    
    }
    
    @end


          直接看上面的代码可能有点乱,你能够通过下载源码执行后查看。

    这个也能够作为界面的架构直接使用。可是假设你想用storyboard来开发,也是极为方便的。


    github主页:https://github.com/chenyufeng1991  。欢迎大家訪问!

    近期极客学院Wiki正在进行IT职业技能图谱的制定,我主要负责iOS方向,大家感兴趣的能够一起參加,有问题或者改动能够直接给我发issues或者pull request。https://github.com/chenyufeng1991/skillmap  。


  • 相关阅读:
    Oracle数据库备份与恢复的三种方法
    mybatis内置类型
    mybatis中#{}和${}的区别
    Statement和PreparedStatement的区别; 什么是SQL注入,怎么防止SQL注入?
    java web简单权限管理设计
    Flask总结篇
    Django总结篇
    API总结
    实战小练习
    数据操作
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7137879.html
Copyright © 2011-2022 走看看