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 。
iOS开发——代码生成TabBar与视图切换具体解释的更多相关文章
- iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇
前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...
- iOS开发:使用Tab Bar切换视图
iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...
- IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例
IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...
- iOS开发之虾米音乐频道选择切换效果分析与实现
今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...
- iOS开发之——从零开始完成页面切换形变动画
前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...
- iOS开发-iPad侧边栏Tab选项卡切换
Android中习惯了叫侧边栏,iOS中如果不习惯侧边栏称呼的话可以叫dock,侧边栏的切换,类似于Android中的底部导航栏的切换,iPad尺寸大了一些,导航的栏目放在侧边会显示的更好耐看一些.选 ...
- iOS开发之窗口和视图
视图就是应用程序的界面.视图可以使用nib文件实现,也可以使用代码创建.一个视图也是一个响应器(UIResponder的子类)这意味着一个视图可以与用户交互.因此,视图不只是用户可看到的界面,也是可以 ...
- iOS开发之视差滚动视图
首先声明一点,由于自己iOS开发经验有限,这里给下面将要实现的效果起名叫视差滚动视图,自己也不知道是否严谨,等以后有经验了,再来更新吧. 一.需求 有的时候我们可能会有这样一种需求,在一个UITabl ...
- iOS开发隐藏tabBar的问题
开发中遇到第一个页面需要显示tabBar,但是第二个页面不需要显示,当回到第一个页面的时候又需要显示的情况. 在第一个页面跳转到第二个页面的时候需要给第二个页面设置tabBar的隐藏 - (void) ...
随机推荐
- iOS基础笔试题 - 集锦一
前言 下文转载自https://mp.weixin.qq.com/s?__biz=MzA4ODk0NjY4NA==&mid=454115946&idx=1&sn=c7f1b50 ...
- LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...
- vs2012 jsoncpp 链接错误
解决: 项目->属性->C/C++->代码生成->运行库->设置与使用的.lib的版本一致.
- Android RecyclerView 滑动时图片加载的优化
RecyclerView 滑动时的优化处理 在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.resume.这里为了避免重复设置增加开销,设置了一个标志变量 ...
- px-em-rem单位转换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ps 快捷键全解
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...
- LeetCode第63题--不同路径
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”).现在考虑网格中 ...
- php file_get_contents函数分段读取大记事本或其它文本文件
当我们遇到文本文件体积很大时,比如超过几十M甚至几百M几G的大文件,用记事本或者其它编辑器打开往往不能成功,因为他们都需要把文件内容全部放到内存里面,这时就会发生内存溢出而打开错误,遇到这种情况我们可 ...
- Compute和Linq的Field使用
目录: Compute的使用 Field的使用 1.Compute 案例: private void ComputeBySalesSalesID(DataSet dataSet) { // Presu ...
- jenkins自动部署测试环境
构建脚本如下: echo "当前目录":$(pwd)echo "当前时间":$(date +%Y-%m-%d_%H:%M)find ./ -type f -na ...