今天我们要学习Navigation Controller和Tab bar Controller。

Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器。
UINavigationController从上往下看,由Navigation bar ,Navigation View ,Navigation toobar等组成。 
页面跳转(两种类型):NavigationViewController跳转(向右前进,向左返回)、ViewContorller跳转(自上而下)。
第一种跳转:通过StoryBoard设置界面的步骤:通过前页的button创建segue(有了segue,push方法才能生效),在前页的viewController中创建button对应的action方法,在方法里面——>先创建一个Storyboard的对象,然后设置viewController的storyboard identifier,通过Storyboard的对象创建刚才id号对应的viewController对象,就可以通过pushViewController跳转到指定的ViewController
let myStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc2 = myStoryboard.instantiateViewController(withIdentifier: "Sec") as! SecViewController
self.navigationController?.pushViewController(vc2, animated: true) let vc3 = myStoryboard.instantiateViewController(withIdentifier: "Third") as! ThirdViewController
self.navigationController?.pushViewController(vc3, animated: true)

然后通过popViewController返回到指定的viewController(从右侧进入):

@IBAction func btn(_ sender: UIButton) {
//前提:由pushView方法跳转而来,三种方法同时只能用一种
//方式1:跳转到前一个页面
// _ = self.navigationController?.popViewController(animated: true)
//方式二:返回到第一个ViewContoller
// _ = self.navigationController?.popToRootViewController(animated: true)
//方式三:跳转到指定页面
for i in 0..<(self.navigationController?.viewControllers.count)! {
if self.navigationController?.viewControllers[i].isKind(of: SecViewController.self) == true {
_ = self.navigationController?.popToViewController(self.navigationController?.viewControllers[i] as! SecViewController, animated: true)
break
}
}

第二种——自下而上:向上弹出,向下返回(通过弹出模式框的方式自下而上的打出viewController):

@IBAction func btn(_ sender: UIButton) {
let myStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc2 = myStoryboard.instantiateViewController(withIdentifier: "Sec") as! SecViewController
self.present(vc2, animated: true, completion: nil)
}

这一个就是真的“返回”了:

@IBAction func backward(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}

通过segue向下页跳转(iOS8之后):Action Segue:Show、Show Detail、Present Modally、Present As Popover、Custom。(具体含义,遇再论)。

在顶部Navigation Bar的Navigation Item(下级页面返回键)——是谁的返回键,就返回到谁:

修改其返回键的显示:

1、默认显示该Item的Title;2、也可以用Back Button更为灵活的显示(设置这个就显示这个,不再通过Title显示,就可以不与其冲突等等)

返回页的Navigation Item如果没有,就应该在该页的顶部Navigation Bar添加一个。

在底部Navigation toolbar的Bar Button Item:

在控件库中,将Bar Button Item拖到底部,可以自定义,也可以选择系统提供的图标库。

Navigation Controller的传值方式:

1、局部变量

2、全局变量

3、MVC模式(model以NSObject为父类)

然后通过segue进行ViewController之间的传值,所以一定要设置segue的Identifier:

前页向后页传值(在前页的prepare方法中进行传值):

局部变量方式:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "SecView" {
let SecView = segue.destination as! SecViewController
SecView.no = self.txt.text
}
}

在后页(接受页):可以在viewDidLoad方法中接受——>self.txt.text = no。

全局变量方式(在appDelgate中,设置全局变量var globle: String?):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "SecView" {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.globle = self.txt.text
}
}

MVC模式(前页后页都要声明model的变量,而且一定要在适当的时候实例化,所以model类一定要写init方法):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "SecView" {
stu = StuEnity(self.txt.text!)
let SecView = segue.destination as! SecViewController
SecView.stu = stu!
}
}

还有一种比较简单、也比较常用的方法——使用单例:

首先创建单例:

final class Single: NSObject {
//创建单例有两种方法
//使用全局变量创建单例
static let shared = Single()
public var name = ""
private override init() {
super.init()
} //类方法,使用结构体创建单例
class func shareInstance() -> Single {
struct single{
static var g_Instance = Single()
}
return single.g_Instance
}
}

然后前页可以向后页传值,后页也可以向前页传值:

//前页的代码:
class ViewController: UIViewController { let data = Single.shared @IBOutlet weak var nameLabel: UITextField! override func viewDidLoad() {
super.viewDidLoad()
self.nameLabel.text = data.name
} override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
data.name = self.nameLabel.text!
} }
//后页的代码:
class SecViewController: UIViewController { let data = Single.shared @IBOutlet weak var nameTextField: UITextField! override func viewDidLoad() {
super.viewDidLoad()
self.nameTextField.text = data.name
} @IBAction func back(_ sender: UIButton) {
data.name = self.nameTextField.text!
let stb = UIStoryboard(name: "Main", bundle: nil)
let back = stb.instantiateViewController(withIdentifier: "FirstView")
self.present(back, animated: true, completion: nil)
} }

先放一个简易版本:http://files.cnblogs.com/files/quanxi/test.zip。

然后还是将使用单例的简单例子放上来:http://download.csdn.net/download/leaf_and_wind/9726433

iOS第八课——Navigation Controller和Tab bar Controller的更多相关文章

  1. Tab Bar Controller和Navigation Controller混合使用详细教程

    在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...

  2. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed

    iOS [错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'] 错误:'Ch ...

  3. tab bar controller

    下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...

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

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

  5. Iphone [Tab Bar实现多view切换,Picker,DataPicter实现

    用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...

  6. Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

    先上效果图: 假设 tab bar items 有5个.tag为0,1,2,3,4.storyboard中tab bar controller继承的class叫做xxxVC. class xxxVC: ...

  7. 学习笔记:Tab Bar 控件使用详解

    注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...

  8. Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

    UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...

  9. Python第八课学习

    Python第八课学习 www.cnblogs.com/resn/p/5800922.html 1 Ubuntu学习 根 / /: 所有目录都在 /boot : boot配置文件,内核和其他 linu ...

随机推荐

  1. 用Spring Boot颠覆Java应用开发

    Java开发概述: 使用Java做Web应用开发已经有近20年的历史了,从最初的Servlet1.0一步步演化到现在如此多的框架,库以及整个生态系统.经过这么长时间的发展,Java作为一个成熟的语言, ...

  2. 25M时钟的梁祝演奏

    module liangzhu(clk_50M,rst,speaker); input clk_50M,rst; output speaker; reg speaker; //分频计数器paramet ...

  3. ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

    本文将介绍使用NLOG.Elmah结合ElasticSearch实现分布式日志管理. 一.ElasticSearch简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布 ...

  4. Java子类属性继承父类属性

    public abstract class Parent { String name = "parent"; } public class Son extends Parent{ ...

  5. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  6. 更新整理本人所有博文中提供的代码与工具(Java,2013.11)

    为了更方便地管理博文中涉及的各种代码与工具资源,现在把这些资源迁移到 Google Code 中,有兴趣者可前往下载. Java 1.<高效 Java Web 应用开发框架 JessMA v3. ...

  7. iOS 大文件断点下载

    iOS 在下载大文件的时候,可能会因为网络或者人为等原因,使得下载中断,那么如何能够进行断点下载呢? // resumeData的文件路径 #define XMGResumeDataFile [[NS ...

  8. python之ATM

    每次做一点就发出来,大神不要嫌重复 2016/11/4 今天来搞ATM,反正逃不了的,说来惭愧,这个作业是我10/4号20天前拿到的,当时是万脸蒙比的,今天又做了一点,现在算是百脸蒙比吧. 一.需求: ...

  9. [LoadRunner]LR11安装或破解时报错的解决方法

    背景:在性能测试项目上,需要安装到LR11进行性能测试,而在安装时会出现安装运行环境失败,安装文件缺失等问题.现总结了上述问题,并给出以下解决方法: 问题1:安装LoadRunner时出现“计算机缺少 ...

  10. Openstack api 学习文档 & restclient使用文档

    Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...