我在之前多篇博客中解说了在不使用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  。

iOS开发——代码生成TabBar与视图切换具体解释的更多相关文章

  1. iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇

    前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...

  2. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  3. IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例

    IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...

  4. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

  5. iOS开发之——从零开始完成页面切换形变动画

    前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...

  6. iOS开发-iPad侧边栏Tab选项卡切换

    Android中习惯了叫侧边栏,iOS中如果不习惯侧边栏称呼的话可以叫dock,侧边栏的切换,类似于Android中的底部导航栏的切换,iPad尺寸大了一些,导航的栏目放在侧边会显示的更好耐看一些.选 ...

  7. iOS开发之窗口和视图

    视图就是应用程序的界面.视图可以使用nib文件实现,也可以使用代码创建.一个视图也是一个响应器(UIResponder的子类)这意味着一个视图可以与用户交互.因此,视图不只是用户可看到的界面,也是可以 ...

  8. iOS开发之视差滚动视图

    首先声明一点,由于自己iOS开发经验有限,这里给下面将要实现的效果起名叫视差滚动视图,自己也不知道是否严谨,等以后有经验了,再来更新吧. 一.需求 有的时候我们可能会有这样一种需求,在一个UITabl ...

  9. iOS开发隐藏tabBar的问题

    开发中遇到第一个页面需要显示tabBar,但是第二个页面不需要显示,当回到第一个页面的时候又需要显示的情况. 在第一个页面跳转到第二个页面的时候需要给第二个页面设置tabBar的隐藏 - (void) ...

随机推荐

  1. Memcached通信协议

    英文水平很烂,做梦都想着能把英语学习,可以使用一口流利的英文和洋鬼子交流,顺便忽悠下自己的同胞.没有地方学习英语,看还可以,网上有很多关于计算机的英文文献,写还行,说就完全不可能了.在以后的工作中慢慢 ...

  2. 前端零基础快速入门JavaScript

    JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html><head> <script&g ...

  3. GC策略

           JVM里的GC(Garbage Collection)的算法有很多种,如标记清除收集器,压缩收集器,分代收集器等等,详见HotSpot VM GC 的种类 现在比较常用的是分代收集(ge ...

  4. jQuey中的return false作用是什么?

    jQuey中的return false作用是什么?在众多的语句中都有return false的使用,当然对于熟悉它的开发者来说,当然是知根知底,知道此语句的作用,当然也就知道在什么时候使用此语句,不过 ...

  5. freopen的各种错误姿势

    本弱鸡已经触发的错误姿势: freoprn("railway.in","r",stdin); freopen("railway.cpp",& ...

  6. Laravel5.5 综合使用

    使用 Laravel5.5 开发一个自动交割的项目,把使用到的开源扩展包及特性整理起来,以供后续使用. 一.安装IDE提示工具 Laravel IDE Helper 是一个极其好用的代码提示及补全工具 ...

  7. mysql连接错误解决(ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol ref used (client option 'secure_auth' enabled))

    当使用mysql的新版本是,连接老版本的mysql,就会有可能报: ERROR 2049 (HY000): Connection using old (pre-4.1.1) authenticatio ...

  8. 统计nginx日志里每五分钟的访问量

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee import time import os import re cla ...

  9. ch12 GUI

    <Head First Java 2nd Edition> 摘录 JFrame 代表屏幕上的一个窗口,可以把 buttons, checkboxes, test fields 等等界面相关 ...

  10. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...