本文代码基于xcode9.2, swift4.0
Navigation跳转流程
适用于有树形层级关系的导航逻辑,类似入栈出栈,最底层是NavigationController,每新打开一页,会将先打开的页压入栈中;每关闭一页,会将该页从栈顶移除,然后显示栈顶页
navigate_jump.jpg实现代码
FirstViewController.swift 跳转下一页代码:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainStoryboard = appDelegate.mainStoryBoard
let secondVC = (mainStoryboard?.instantiateViewController(withIdentifier: "secondVC"))!
self.navigationController?.pushViewController(secondVC, animated: true)
SecondViewController.swift 和 ThirdViewController,跳转下一页和FirstViewController一样
FourthViewController.swift 跳转上一页代码:
self.navigationController?.popViewController(animated: true)
FourthViewController.swift 跳转至第二页代码:
let controller = (self.navigationController?.viewControllers[1])!
self.navigationController?.popToViewController(controller, animated: true)
FourthViewController.swift 跳转至根页代码:
self.navigationController?.popToRootViewController(animated: true)
用法总结
-
通过storyboard初始化UIViewController用法
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) //根据name获取对应的storyboard let viewController = mainStoryboard.instantiateViewController(withIdentifier: "firstvc") //根据Storyboard Id获取对应的ViewController
-
navigateController跳转用法
self.navigationController?.popViewController(animated: true) //跳转上一页 self.navigationController?.popToViewController(controller, animated: true) //跳转至指定页 self.navigationController?.popToRootViewController(animated: true) //跳转至最底页
Present跳转流程
适用于详情、评价等无下级层次的逻辑
实现代码
FirstViewController.swift 打开SimpleViewController页:
self.present(simpleVC, animated: true, completion: nil)
SimpleViewController.swift关闭本页
self.dismiss(animated: true, completion: nil)
网友评论