NotificationCenter for data transfer in Swift

I am working on a test project in Swift 3. I am trying to pass a textField string from one class to another class using NotificationCenter. I am trying to execute the answer at this link: pass the NSString variable to another class with NSNotification and how to pass multiple values ​​using quick notification

I tried several answers from the link above, but nothing worked.

My code is:

// First VC

import UIKit extension Notification.Name { public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey") } class ViewController: UIViewController { @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func sendData(_ sender: AnyObject) { let userInfo = [ "text" : textView.text ] NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo) } } 

// SecondVC

  import Foundation import UIKit class viewTwo: UIViewController { @IBOutlet weak var result: UILabel! override func viewDidLoad() { } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(self.notificationReceived(_:)), name: .myNotificationKey, object: nil) } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) NotificationCenter.default.removeObserver(self, name: .myNotificationKey, object: nil) } func notificationReceived(_ notification: Notification) { guard let text = notification.userInfo?["text"] as? String else { return } print ("text: \(text)") result.text = text } 

enter image description here }

I am not sure what is wrong with the code. Above, the code was initially marked as the answer I found from the first link. The code has been converted to Swift.

+5
source share
3 answers

Do not use the object parameter to transfer data. It is designed to filter notifications with the same name, but from a specific object. Therefore, if you transfer an object when you send a notification and another object when you add Observer, you will not receive it. If you pass zero, you basically turn off this filter.

Instead, you should use the userInfo parameter.

First, it’s best to define the name of the notification as an extension for Notification.Name. This approach is much safer and more readable:

 extension Notification.Name { public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey") } 

Message:

 let userInfo = [ "text" : text ] NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo) 

Subscribe:

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(self.notificationReceived(_:)), name: .myNotificationKey, object: nil) } 

Unsubscribe

 override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) NotificationCenter.default.removeObserver(self, name: .myNotificationKey, object: nil) } 

Method call:

 func notificationReceived(_ notification: Notification) { guard let text = notification.userInfo?["text"] as? String else { return } print ("text: \(text)") } 
+6
source

Pass text using userInfo , which is an optional dictionary like [AnyHashable: Any]? in Swift 3.0 and [NSObject: AnyObject]? in swift 2.0

 @IBAction func sendData(_ sender: UIButton) { // post a notification NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: ["text": textValue.text]) print(textValue) // textValue printing } 

in viewDidLoad

// Register to receive notifications

 NotificationCenter.default.addObserver(self, selector: #selector(self. incomingNotification(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil) 

and inbound Notification

 func incomingNotification(_ notification: Notification) { if let text = notification.userInfo?["text"] as? String { print(text) // do something with your text } } 
+1
source

In the sendData method sendData go textField.text to the notification object and in incomingNotification do the following:

 guard let theString = notification.object as? String else { print("something went wrong") return } resultLabel.text = theString 

You can also use blocks to transfer data between controllers.

0
source

Source: https://habr.com/ru/post/1258125/


All Articles