如何使用通知中心将数据从第三个 viewcontroller 向后移动到第一个 viewcontrlloer?
How to move data backward from third viewcontroller to first viewcontrlloer using notification center?
我有 3 个视图控制器。我已经将对象从第一个传递到第二个,从第二个传递到第三个 viewcontroller,现在在第三个 viewcontroller 中编辑一些数据,并尝试使用通知中心将更新的数据对象发送回第一个 viewcontroller。但是我不确定我应该如何或使用哪种方法来 post 可以将数据对象传递给第一个 viewcontrller 的通知。
现在我尝试如下
// 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 ???
}
}
你需要
let abc = notification.object as! Contact
我有 3 个视图控制器。我已经将对象从第一个传递到第二个,从第二个传递到第三个 viewcontroller,现在在第三个 viewcontroller 中编辑一些数据,并尝试使用通知中心将更新的数据对象发送回第一个 viewcontroller。但是我不确定我应该如何或使用哪种方法来 post 可以将数据对象传递给第一个 viewcontrller 的通知。
现在我尝试如下
// 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 ???
}
}
你需要
let abc = notification.object as! Contact