ios segue navigation between view controllers
1. Create four View Controllers on the storyboard.
2. Select the first View Controller and add a navigation controller to it by, Editor -> Embed in -> Navigation Controller.
3. Create three View Controller classes, AViewControler.swfit, BViewController.swift, CViewController.swift, and DViewController.swift.
4. Assign the View Controller classes to the View Controller object on the storyboard, by select the View controller on the storyboard, and select the a class in the identity inspector.
5. Add a button to each of View Controllers except the last one.
6. Add two labels to all View Controllers.
7. Add outlet to the View Controller class for one of the two labels, name it msgLabel.
8. Ctrl drag the button from View Controller ONE to View Controller TWO, and select show on the storyboard.
9. Ctrl drag the button from View Controller TWO to View Controller THREE, and select show on the storyboard.
10. AViewController.swift should look this. In the prepare method, we are finding the destination view controller from the segue and assigning the message property of the view controller a string.
import UIKit class AViewController: UIViewController { var message = "" @IBOutlet weak var msgLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() msgLabel.text = message } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. if let vc = segue.destination as? BViewController { vc.message = "This is a message from A" } } }
11. BViewController.swift should look this. In the viewDidLoad method, we are initializing the label with the message. This message initialized in the prepare method from the AViewController. In the prepare, we are finding the destination view controller from the segue and assigning the message property of the view controller a string.
import UIKit class BViewController: UIViewController { var message = "" @IBOutlet weak var msgLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() msgLabel.text = message } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. if let vc = segue.destination as? CViewController { vc.message = "This is a message from B" } } }
12. CViewController.swift should look this. In the viewDidLoad method, we are initializing the label with the message. This message initialized in the prepare method from the BViewController. The method goToFour responds to a button tap, it initializes the storyboard by name which is Main in this case. The name has to match with the storyboard name. Then it instantiates the view controller from this storyboard by using the storyboard id of the view controller. The storyboard id need to be assigned to the view controller from the identity inspector, it identifies the unique view controller on a storyboard.
import UIKit class CViewController: UIViewController { var message = "" @IBOutlet weak var msgLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() msgLabel.text = message } @IBAction func goToFour(_ sender: UIButton) { let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let dViewController = storyBoard.instantiateViewController(withIdentifier: "DViewController") as! DViewController dViewController.message = "This is a message from C" navigationController?.pushViewController(dViewController, animated: true) } }
13. DViewController.swift should look this. In the viewDidLoad method, we are initializing the label with the message. This message initialized in the goToFour method from the CViewController.
import UIKit class DViewController: UIViewController { var message = "" @IBOutlet weak var msgLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() msgLabel.text = message } }
Search within Codexpedia
Search the entire web