ios - NotificationCenter 在 Swift 中传递数据

标签 ios swift uiviewcontroller nsnotificationcenter

我正在使用 Swift 3 开发一个测试项目。我正在尝试使用 NotificationCenter 将 textField 字符串从一个类传递到另一个类。我正在尝试从此链接中找出答案:pass NSString variable to other class with NSNotificationhow 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)

}

}

//第二VC

 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

我不确定代码有什么问题。上面,我从第一个链接找到的最初标记为已回答的代码。代码已转换为 Swift。

最佳答案

不要使用对象参数来传递数据。它旨在过滤具有相同名称但来自特定对象的通知。因此,如果您在发布通知时传递了一些对象,而在添加观察者时传递了另一个对象,您将不会收到它。如果你传递 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)")
}

关于ios - NotificationCenter 在 Swift 中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40015748/

相关文章:

iphone - 我怎样才能获得第二个mov播放时间

ios - 在应用商店或 Paypal 或其他地方?

ios - 在字符串 : Swift 中的特定字符之后插入字符

ios - FanMenu 中的按钮位置确定问题

ios - 程序化 UIView 内的程序化 UIButton 无法正常工作

swift - 关闭 UIViewController 后 UI 卡住 5 秒

objective-c - iOS - UIView 边框底部宽度?

ios - 默认纵向,一些特殊的屏幕如何自动旋转或强制某些屏幕横向?

ios - 在 Storyboard中为 'tile' 创建更大的翻转 View

ios - 在 UIViewController 之间导航时遇到问题