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) ...
随机推荐
- 校网助手APP lua源码
import 'android.webkit.WebView'webView.addJavascriptInterface({},'JsInterface') import 'test' cjson= ...
- python自动化--语言基础一数据类型及类型转换
Python中核心的数据类型有哪些?变量(数字.字符串.元组.列表.字典) 什么是数据的不可变性?哪些数据类型具有不可变性数据的不可变是指数据不可更改,比如: a = () #定义元组 #a[]= # ...
- Markdown(github)语法
<< 访问 Wow!Ubuntu NOTE: This is Simplelified Chinese Edition Document of Markdown Syntax. If yo ...
- PHP实现写LOG日志的代码
这篇文章给大家介绍的内容是关于PHP实现写LOG日志的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. public function write_log(){ //设置目录时间 ...
- Java基础概念语法
Java基础概念语法 注释 单行注释 //行注释说明 多行注释 /* 多行注释说明 */ 文档注释 /** *@author 程序的作者 *@version 源文件的版本 *@param 方法的参数说 ...
- 【原】SMTP发送邮件
1.下载class.phpmailer.php和class.smtp.php至公共库 2.编写发邮件的公共函数 function sendMail($param) { $config = C('THI ...
- vue+webpack+npm搭建的纯前端项目
转载来源:https://www.cnblogs.com/shenyf/p/8341641.html 搭建node环境 下载 1.进入node.js官方网站下载页,点击下图中框出位置,进行下载即可,当 ...
- react 导航切换
<ul class="nav"> <li onClick={() => this.changeFontColor(0)} className={`${0 = ...
- Scrapy 组件的具体用法
一.Spider 用法 在 Scrapy 中,要抓取网站的链接配置.抓取逻辑.解析逻辑都是在 Spider 里完成的.Spider 的一些基础属性和基础方法: name:爬虫名字,Spider的名字定 ...
- C++ volatile关键字(转)
文章来源: http://hedengcheng.com/?p=725 https://www.cnblogs.com/god-of-death/p/7852394.html 1.基本概念 volat ...