我正在研究Swift 3中的测试项目。我正在尝试使用NotificationCenter将textField字符串从一个类传递到另一个类。我正试图通过以下链接来解答答案:pass NSString variable to other class with NSNotification和how to pass multiple values with a notification in swift
我从上面的链接尝试了几个答案,但没有任何效果。
我的代码:
//第一次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
}
我不确定代码有什么问题。上面,代码最初标记为答案,我从第一个链接中找到。代码已转换为Swift。
答案 0 :(得分:19)
不要使用object参数传递数据。它旨在过滤具有相同名称但来自特定对象的通知。因此,如果在addObserver发布通知和另一个对象时传递了一些对象,则不会收到它。如果你传递nil,你基本上会关闭这个过滤器。
您应该使用userInfo
参数。
首先,最好将通知的名称定义为Notification.Name的扩展名。这种方法更安全,更易读:
extension Notification.Name {
public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey")
}
发布通知:
let userInfo = [ "text" : text ]
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo)
订阅:
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)")
}
答案 1 :(得分:4)
使用userInfo
传递文本,这是一个可选的字典类型[AnyHashable:Any]?在Swift 3.0中它是[NSObject:AnyObject]?在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
}
viewDidLoad
中的
//注册接收通知
NotificationCenter.default.addObserver(self, selector: #selector(self. incomingNotification(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
和incomingNotification
func incomingNotification(_ notification: Notification) {
if let text = notification.userInfo?["text"] as? String {
print(text)
// do something with your text
}
}
答案 2 :(得分:1)
在sendData
方法传递textField.text
到Notification对象和incomingNotification
中执行此操作:
guard let theString = notification.object as? String else {
print("something went wrong")
return
}
resultLabel.text = theString
您还可以使用块在控制器之间传递数据。
答案 3 :(得分:1)
使用dispatchQueue
,因为您的通知是在加载视图之前发布的。因此,请延迟通知邮件。
@IBAction func sendData(_ sender: AnyObject) {
let userInfo = [ "text" : textView.text ]
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo) }
}