-1

I have 3 view controller. I have passed object from first to second and second to third viewcontroller and now editing some data in third viewcontroller and trying send updated data object back to first viewcontroller using notification center. But I am not sure how or which method should i use to post notification which can pass data object to first viewcontrller.

For now I tried as following

    // third view controller 
    import UIKit

    protocol editContactDelegate: AnyObject {
        func updateContact(contact: Contact)
    }

    class EditViewController: UIViewController {

        @IBOutlet weak var lblName: UILabel!
        @IBOutlet weak var lblPhoneNumber: UILabel!
        @IBOutlet weak var lblEmailID: UILabel!



        var editContact: Contact?

        weak var delegate: editContactDelegate?

        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.

            lblName.text = editContact?.name
            lblPhoneNumber.text = editContact?.phone
            lblEmailID.text = editContact?.email

        }

        @IBAction func editContact(_ sender: AnyObject) {


///??? which method should i use to post object 
         NotificationCenter.default.post(name: NSNotification.Name("Test"), object: Contact(name: "abc", position: "xyz", email: "updated@gmail.com", phone: "updated number"))
        }

    }

//////////

// first view controller 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, editContactDelegate {
    func updateContact(contact: Contact) {
        print(contact.email)
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.tableview.delegate =  self
        self.tableview.dataSource = self

        tableview.register(UINib(nibName: "ContactCell", bundle: nil), forCellReuseIdentifier: "ContactCell")

       NotificationCenter.default
            .addObserver(self,
                         selector:#selector(NotificationAct(_:)),
                         name: NSNotification.Name ("Test"),                                          object: nil)

    }


    @objc func NotificationAct(_ notification: NSNotification) {

        var abc = notification. // how to get object from third viewcontroller ???

    }
}
New iOS Dev
  • 1,937
  • 7
  • 33
  • 67
  • Consider to unwind the segue to the first controller. That's more appropriate than Notification. – vadian Dec 26 '19 at 15:56
  • @vadian thanks. Sure unwind is more elegant but I want to understand this concept using notification center. Just for learning purpose. can you please help – New iOS Dev Dec 26 '19 at 16:03
  • `Notification` is only useful if the controllers are not related at all or you want to send the notification to multiple objects. – vadian Dec 26 '19 at 16:06

1 Answers1

-1

You need

let abc = notification.object as! Contact
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87