將許多ViewController拉到main.storyboard,之後以拖拉的方式將要實現Segue的元件拉到要進行segue的ViewController,選Action Segue -> Show 。註:此法如果沒有特別設定則在導覽時會是由下往上轉換畫面。
/下列兩個方法可以不用特別設定就產生橫向導覽的型態/
選擇Editor -> Embed in -> Navigation Controller 。
直接在右下方選取Navigation Controller,之後可先把預設的Table View Controller先刪掉,然後從Navigation Controller按住Control拖拉到新的ViewController,然後選root view controller。註:Navigation Controller 包住的第一個ViewController稱為“ root view controller”。
註1:方法2與3產生的畫面上面會產生一navigation bar,若要在上面新增按鈕,要選Bar Button Item(不是選Button)。
註2:於navigation bar可以設定title(預設只有root view controller可以直接設定),其他的畫面必須再拉進元件Navigation Item。
全部做好後的結果
import UIKit
class ViewController: UIViewController {
@IBAction func gotoView2(_ sender: UIButton) {
let lightRed = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "lightRed")
//用class UIStroyboard的instantiateViewController方法可以初始化出一個UIViewController,在withIdentifier填入剛剛設定的storyboard ID,並將此UIViewController實體化存進lightRed這個常數
// present(lightRed, animated: true, completion: nil)
//將lightRed這個ViewController呈現出來,但這樣會讓轉場是從下到上
/*想讓轉場為橫向的,但要這樣做必須先將第一個畫面embed in 到navigation viewController*/
self.navigationController?.pushViewController(lightRed, animated: true) //此行意義用self.navigationController回到包著我的navigationController(型別為class UINavigationController),再利用UINavigationController中apple內建的方法pushViewController,推到我們設定所要的ViewController(利用storyboardID推)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}*
import UIKit
class LightGreenViewController: UIViewController {
@IBAction func backtoView2(_ sender: UIButton) {
// self.navigationController?.popViewController(animated: true) //此行意義用self.navigationController回到包著我的navigationController(型別為class UINavigationController),再利用UINavigationController中apple內建的方法popViewController回到上一個畫面
self.navigationController?.popToRootViewController(animated: true) //回到第一個畫面
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
註:注意到從lightRed要到lightGreen採用橫向切換,因此有此行程式碼*self.navigationController?.pushViewController(lightRed, animated: true)
-> 推出下一個畫面
而從lightGreen回到lightRed我們採用下述程式碼*self.navigationController?.popViewController(animated: true)*
但如果現在從lightRed到lightGreen不是採用橫向轉場,而是縱向轉場*present(lightRed, animated: true, completion: nil)*
,沒有推出下一個畫面而是用呈現的,這樣會連帶影響lightGreen回到lightRed的程式碼*self.navigationController?.popViewController(animated: true)*
失效。
解決方法是用dismiss方法:dismiss(animated: true, completion: nil)
取代上面的程式碼,但這樣轉場都會變成縱向的
performSegue(withIdentifier: String, sender: <#T##Any?#>)
,其中Identifier填入剛剛的ID,sender是要傳的參數,此方法預設的轉場方式是由下至上。如果要橫向轉場,將第一個畫面Embed in到Navigation Controller。var infoFromViewOne:String?
,為了讓值從第一個畫面傳到第二個畫面而設定的。performSegue(withIdentifier: "自己設定的", sender: myInput /*傳出myInput這個資料出去*/)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
GitHub位置:https://github.com/ethan510010/SceneChangeWithNavigation.git
GitHub位置:GitHub - ethan510010/ScenePicker