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: "[email protected]", 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 ???
}
}
You need
let abc = notification.object as! Contact