Chào bạn,

Đăng nhập xem việc làm phù hợp

Blog IT

Bên cạnh Delegate, chúng ta còn có thể sử dụng Unwind Segue

Bên cạnh Delegate, chúng ta còn có thể sử dụng Unwind Segue

Delegate là một design pattern rất cơ bản và phổ biến trong lập trình iOS, trong khi đó, không có nhiều lập trình viên biết đến Unwind Segue trong lập trình iOS, mặc dù cả 2 đều có chung một đặc điểm là dismiss View Controller hiện tại và truyền dữ liệu trả về View Controller trước đó. Bài viết hôm nay sẽ tìm hiểu hơn về Unwind Segue, so sánh và thay thế Delegate protocol trong việc truyền dữ liệu giữa các View.

Delegate

Như tôi đã nói ở trên thì Delegate là một pattern rất phổ biến trong công việc truyển dữ liệu giữa các View. Cơ chế hoạt động của nó là View B ủy quyền cho View A thực thi một hành động nào đó, View A sau khi nhận dữ liệu View.B trả về, sẽ dismiss View B và thực thi hành động mà view B đã ủy quyền trước đó. Lấy ví dụ tôi có một vài property ở Master View Controller và muốn hiển thị, thay đổi chúng tại Detail View Controller.

Tại Detail View Controlelr, tôi tạo ra một protocol mà Vien Controlelr hiện tại phải tuân thủ để díismiss Detail View Controller:

protocol DetailViewControllerDelegate: class {
  func didDismissDetail(sender: DetailViewController)
}

Đồng thời tôi cũng phải khai báo một đối tượng delegate 

weak var delegate:DetailViewControllerDelegate?

Bây giờ ở sự kiện mà sẽ làm biến mất Detail View Controller, chúng ta gọi tới delegate vừa khai báo và truyền self như một tham số sender:

@IBAction private func doneAction(sender: UIBarButtonItem) {
  delegate?.didDismissDetail(self)
}

Tại View Controller hiện tại, tuân thủ protocol và thực thi delegate để nhận data và dismiss Detail View Controller:

extension ViewController: DetailViewControllerDelegate {
  func didDismissDetail(sender: DetailViewController) {
    status = sender.status
    dismissViewControllerAnimated(true, completion: nil)
    configureView()
  }
}

Đồng thời cũng nhớ set delegate khi hiển thị Detail View Controller tại phương thức prepareForSegue:

 // Pass data into the view controller and set
 // ourselves as the delegate
 detailViewController.status = status
 detailViewController.delegate = self

Unwind segue là gì?

Unwind segue cho phép chúng ta quay trở về với View Controller phía trước trong cây navigation. Bạn tạo một unwind segue ở trong Interface Builder nhưng không giống những segue bình thường, chúng ta không đặt điểm đich (destination) của segue đó. Thay vào đó segue sẽ tìm trong runtime một View Controller gần nhất trong cây navigation mà thực thi hành động unwind dó.

Một unwind action chỉ có 2 property duy nhất:

  • Một kiểu return của IBAction
  • Một tham số dạng UIStoryboardSegue

Lưu ý rằng khi thiết lập một unwind segue tại Interface Builder, tất cả những unwind action hợp lệ sẽ được liệt kê ra, do vậy bạn cần đặt một cái tên có đầy đủ ý nghĩa và dễ phân biệt. Để thêm một unwind segue, ta làm như sau:

@IBAction func unwindToMainViewController(sender: UIStoryboardSegue) {
  if let sourceViewController = sender.sourceViewController
                                as? DetailViewController {
    status = sourceViewController.status
    configureView()
  }
}

Tham số UIStoryboardSegue cho phép chúng ta truy cập vào View Controller tại property sourceViewController. Co chế ở đây cũng giống như là delegate protocol, truyền dữ liệu về trước khi dismiss View Controller.

Để add unwind segue vào trong Storyboard, chúng ta giữ control và kéo thả từ nút Done tại Detail View Controller tới icon Exit màu da cam ở trên scene menu bar:

Interface Builder sẽ hiển thị tất cả những unwind action mà nó biết, tại trường hợp này chúng ta chỉ có một segue đã tạo trước đó:

Bạn cũng có thể nhìn thấy segue được list tại outline của scene:

Nút Done bây giờ sẽ gọi đến unwind segue thực thi nhiệm vụ dismiss View controller và tim tới phương thức unwindToMainViewController ở view controller hiện tại.

Lời kết

Compared to the delegate approach this is less code but is it any better? It needs storyboards which I know is a non-starter for some. However not having to create the delegate protocol is a big time saver. What do you think, do you use unwind segues and if so when?

So sánh với Delegate thì chúng ta có thể thấy Unwind segue ít code hơn, nhưng bên cạnh đó thì nó cũng cần phải dùng đến Storyboards. Tuy nhiên thì unwind segue cũng giúp chúng ta tiết kiệm được thời gian hơn so với việc thiết lập Delegate. Bài viết trên tôi đã liệt kê và so sánh 2 phương pháp qua đó sẽ giúp các bạn chọn được phương pháp phù hợp với sở thích và cấu trúc dự án của mình.

Nguồn: Techmaster

Bài viết tương tự

Bài viết nổi bật

Công việc liên quan