Segue Là Gì

Xin kính chào chúng ta, bây giờ vào bài viết này mình sẽ giới thiệu các để truyền dữ liệu xuất phát điểm từ 1 VC A -> VC B và ngược lại sử dụng segue.

Bạn đang xem: Segue là gì

Truyền dữ liệu tự A -> B

Sử dụng SHOW SEGUE

Mình là 1 trong người ưu tiền về việc áp dụng file xib, storyboard. Nlỗi trong một dự án, Khi bạn muốn dịch rời mang đến một màn hình hiển thị thì bản thân đã kéo segue trên storyboard mang đến nhanh. Sau lúc đã kéo hoàn thành bản thân vẫn đặt tên mang lại segue identifier (sử dụng nhằm xác định xem chính là storyboard segue nào).

*
Trong thời điểm này mình đang có 2 VC là A cùng B. Từ VC A mình bấm vào button vẫn dịch chuyển sang VC B và truyền theo 1 đoạn text thì bản thân vẫn làm cho nlỗi sau:

Trong VC A bản thân override lại func prepare for segue, sinh sống vào function này mình trọn vẹn rất có thể kiểm tra theo segue identifier hoặc không, bản thân rất có thể truyền dữ liệu mang lại VC nhưng mà segue trỏ đến.,

import UIKitclass ViewControllerA: UIViewController override func viewDidLoad() super.viewDidLoad() // Do any additional thiết đặt after loading the view, typically from a nib. override func prepare(for segue: UIStoryboardSegue, sender: Any?) guard let vcB = segue.destination as? ViewControllerB else return vcB.initData(text: "Demo") Trong VC B, mình khởi chế tạo ra một function initData cùng một property tên là text để hứng tài liệu trường đoản cú VC A. Hàm initData sẽ tiến hành Gọi trươc lúc viewDidLoad được call vậy phải chúng ta luôn luôn rước đc text không giống trống rỗng.import UIKitclass ViewControllerB: UIViewController var text = ""
IBOutlet weak var lbText: UILabel! override func viewDidLoad() super.viewDidLoad() // Do any additional thiết đặt after loading the view. self.lbText.text = text func initData(text: String) self.text = text

Sử dụng modal segue

Trong dự án công trình, bài toán sử dụng modal segue thường xuyên là bạn muốn present lên một VC B được embed trong navigation controller riêng biệt với VC hiện tại. Dựa vào ví dụ trên, mình đang sửa lại storyboard và code vào VC A.
*

Trong VC A bây giờ bản thân đã sửa code lại như sau:

import UIKitclass ViewControllerA: UIViewController override func viewDidLoad() super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. override func prepare(for segue: UIStoryboardSegue, sender: Any?) if let navigationController = segue.destination as? UINavigationController, let editContactViewController = navigationController.viewControllers.first as? ViewControllerB vcB.initData(text: "Demo")

Sử dụng code ráng vày cần sử dụng segue

Hai phương pháp làm việc trên là bản thân thường xuyên thường được sử dụng vị mình là người dân có kinh nghiệm dùng phần lớn gì đã làm được cấp cung cấp sẵn. Trong khi, chúng ta cũng rất có thể viết code để truyền data từ VC A - > VC B
*
Hiện nay bản thân đang xoá đi segue sẽ tạo ra trước đó, và sửa code trong VC A như sau:

import UIKitclass ViewControllerA: UIViewController override func viewDidLoad() super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
IBAction func actionGoToVCB(_ sender: Any) let storyboard = UIStoryboard(name: "Main", bundle: nil) if let vcB = storyboard.instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB vcB.initData(text: "Demo") let navigationViewController = UINavigationController(rootViewController: vcB) show(navigationViewController, sender: nil)

Truyền tài liệu từ B -> A

Sử dụng unwind segue.

Xem thêm:

Trong thời điểm này họ phải truyền dữ liệu ngược trường đoản cú B -> A. Mình thấy Việc áp dụng segue này sẽ gồm hơi thọ, tuy vậy siêu không nhiều chúng ta nghe biết nó.Thì trước tiên, để thực hiện unwind segue, bọn họ đề xuất sản xuất 1 action ngơi nghỉ vào VC A nhỏng sau:

import UIKitclass ViewControllerA: UIViewController
IBOutlet weak var lbPassBackText: UILabel! override func viewDidLoad() super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. override func prepare(for segue: UIStoryboardSegue, sender: Any?) if let navigationController = segue.destination as? UINavigationController, let vcB = navigationController.viewControllers.first as? ViewControllerB vcB.initData(text: "Demo")
IBAction func unwindToVCA(_ unwindSegue: UIStoryboardSegue) if let vcB = unwindSegue.source as? ViewControllerB lbPassBackText.text = vcB.text Tiếp đó mình sửa lại storyboard như ảnh sau:

*
Ở trong storyboard hôm nay, bản thân có một bar button item mang tên là Unwind Segue, bản thân đang phải kéo action của chính nó map với action "unwindToVCA" vào ViewControllerA. Để làm cho được thì bản thân sẽ giữ lại được Ctrl với kéo nó vào phần exit của VC B bên trên storyboard.
*

Giờ vào VC B bản thân đang override thêm func prepare for segue

import UIKitclass ViewControllerB: UIViewController var text = ""
IBOutlet weak var lbText: UILabel! override func viewDidLoad() super.viewDidLoad() // Do any additional cài đặt after loading the view. self.lbText.text = text func initData(text: String) self.text = text override func prepare(for segue: UIStoryboardSegue, sender: Any?) self.text = "LALALAALALA" => Vậy nên là tôi đã hoàn toàn có thể truyền dữ liệu ngược trường đoản cú B -> A.

Kết

Nhỏng làm việc bên trên mình đã đưa ra cách để thực hiện segue trong việc truyền dữ liệu thân 2 Vc A với B. Ngoài cách thực hiện unwind segue thì chúng ta còn rất có thể sử dụng những bí quyết không giống như: delegate, closure...Cảm ơn các bạn đã gọi cho trên đây, hi vọng số đông gì mình share nghỉ ngơi trên sẽ có thể góp chúng ta được một chút ít.