本文代碼基于xcode9.2存哲, swift4.0
Navigation跳轉(zhuǎn)流程
適用于有樹形層級(jí)關(guān)系的導(dǎo)航邏輯窥岩,類似入棧出棧晃琳,最底層是NavigationController邮屁,每新打開一頁库菲,會(huì)將先打開的頁壓入棧中账忘;每關(guān)閉一頁,會(huì)將該頁從棧頂移除熙宇,然后顯示棧頂頁
navigate_jump.jpg
實(shí)現(xiàn)代碼
FirstViewController.swift 跳轉(zhuǎn)下一頁代碼:
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,跳轉(zhuǎn)下一頁和FirstViewController一樣
FourthViewController.swift 跳轉(zhuǎn)上一頁代碼:
self.navigationController?.popViewController(animated: true)
FourthViewController.swift 跳轉(zhuǎn)至第二頁代碼:
let controller = (self.navigationController?.viewControllers[1])!
self.navigationController?.popToViewController(controller, animated: true)
FourthViewController.swift 跳轉(zhuǎn)至根頁代碼:
self.navigationController?.popToRootViewController(animated: true)
用法總結(jié)
-
通過storyboard初始化UIViewController用法
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) //根據(jù)name獲取對(duì)應(yīng)的storyboard let viewController = mainStoryboard.instantiateViewController(withIdentifier: "firstvc") //根據(jù)Storyboard Id獲取對(duì)應(yīng)的ViewController
-
navigateController跳轉(zhuǎn)用法
self.navigationController?.popViewController(animated: true) //跳轉(zhuǎn)上一頁 self.navigationController?.popToViewController(controller, animated: true) //跳轉(zhuǎn)至指定頁 self.navigationController?.popToRootViewController(animated: true) //跳轉(zhuǎn)至最底頁
Present跳轉(zhuǎn)流程
適用于詳情鳖擒、評(píng)價(jià)等無下級(jí)層次的邏輯
實(shí)現(xiàn)代碼
FirstViewController.swift 打開SimpleViewController頁:
self.present(simpleVC, animated: true, completion: nil)
SimpleViewController.swift關(guān)閉本頁
self.dismiss(animated: true, completion: nil)