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) ...
随机推荐
- 轮播图-version1
实现目标 按'>'出现下一caption,按'<'出现上一caption 按下面的点到指定的caption 自动轮播 思路: 设置一个carousel容器,里面有carousel的每一张图 ...
- (1)指针、引用、const限定符
自己看书时的一些理解,可能有错误的地方.随着指针的使用增多,会不断修改这篇文章的内容,过去错误的会用划线划去后保留. 1.对引用.指针.常量引用.指向常量的指针.常量指针的理解 //对引用.指针.常量 ...
- Android RxJava1.X升级到RxJava2.X笔记
简书地址 http://www.jianshu.com/p/2badfbb3a33b 描述 RxJava 1.X RxJava 2.X package包名 rx.xxx io.reactivex.xx ...
- TensorFlow OOM when allocating tensor with shape[5000,384707]
在session范围内不要进行eval()或者convert_to_tensor()操作, 否则会造成OOM,或者报出错误:GraphDef cannot be larger than 2GB usi ...
- 左耳听风 ARTS Week 002
要求:1.每周至少做一个 leetcode 的算法题 2.阅读并点评至少一篇英文技术文章 3.学习至少一个技术技巧 4.分享一篇有观点和思考的技术文章 1.每周至少做一个 leetcode 的算法题 ...
- Spring+Spring MVC+Hibernate环境搭配
Spring+Spring MVC+Hibernate简称"SSH".Spring容器是Spring的核心,该 容器负责管理spring中的java组件.Spring的核心机制:依 ...
- 在Resource中使用x:Bind
Build2015上,MS热情高涨的演示了x:Bind,一种新的Binding方式,新的方式有如下优点: 1更好的性能(内存占用,CPU占用) 2BuildTime的Binding 具体在Channe ...
- 15年第六届蓝桥杯第七题_(string)
手链样式 小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙.他想用它们串成一圈作为手链,送给女朋友.现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢? 请你提交该整数.不要填写任 ...
- 网络编程基础_3.APC队列
APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...
- php利用array_filter()过滤数组空值
利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...