Perform screen transitions with Segue and present. If you press the "Jump with Segue" button on the first screen (View Controller), you will move to the second screen (Second View Controller). If you press the "Jump with present" button on the second page, you will be taken to the third screen (ThirdViewController). The colors are the same as the button color and the background color of the transition destination. . . ViewController . . SecondViewController . . ThirdViewController . .
I'm using the code I made earlier in [Swift] The last saucer for beginners who don't know Delegate. (See there for a detailed delegate description)
ViewController.swift
func onTapYellowButton() {
//Make a screen transition to SecondViewController
self.performSegue(withIdentifier: "toSecondViewController", sender: nil) //Use the same Identifier as storyboad
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier! {
case "toSecondViewController": //Segue Identifier
let secondVC: SecondViewController = segue.destination as! SecondViewController
secondVC.word = "Transitioned with segue" //Pass a value to SecondViewController
break
default:
break
}
}
SecondViewController.swift
var word: String = "";
@IBOutlet weak var label: UILabel?
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow //Make the background color yellow
label?.text = word //to word by prepare"Transitioned with segue"Is included
}
With Segue, you have to connect when changing screens, but with present, you don't have to. Therefore, the screen can be transitioned to another NavigationController. By the way, what is NavigationController? As a supplement for those who say, NavigationController is like a frame. It means that the TableView on it is placed. The image looks like this. So when I use NavigationController, it looks like it has a frame on top.
Aside from that, I'll go into the implementation.
SecondViewController.swift
@IBAction func buttonTap(_ sender: Any) {
let navi: UINavigationController = self.storyboard?.instantiateViewController(withIdentifier: "NaviForController") as! UINavigationController
let nextVC: ThirdViewController = navi.viewControllers[0] as! ThirdViewController
nextVC.word = "Screen transition with present" //Pass a value to the word of ThirdViewController
self.present(navi, animated: true, completion: nil) //Screen transition to ThirdViewController
}
ThirdViewController.swift
var word: String = ""
@IBOutlet weak var label: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green //Make the background color green
label?.text = word //SecondViewController nextVC.word = "Screen transition with present"Is included by
}
How was that? Personally, it was a shock when I first learned that I always go through prepare when using it during Segue. I haven't specified it this time, but it's easy to forget to match the storyboard class with the code class, so you have to remember it. Storyboard and all the code for your reference
Recommended Posts