[英]NotificationCenter.default.addObserver is not calling the objective C function
I am using swift 5 on Xcode 11.0, trying to pass a userInfo from view-controller-A to view-controller-B and I already made a notification center post in view-controller-A and here is the code:我在 Xcode 11.0 上使用 swift 5,试图将 userInfo 从 view-controller-A 传递到 view-controller-B 并且我已经在view-controller-A中发布了通知中心帖子,这是代码:
let uid = response["session-id"] as? String
NotificationCenter.default.post(
name: NSNotification.Name("didReceiveSession"),
object: nil,
userInfo: ["uid" : uid!]
)
And in view-controller-B I registered the notification and added an observer like so:在view-controller-B中,我注册了通知并添加了一个观察者,如下所示:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.getSessionId),
name: NSNotification.Name("didReceiveSession"),
object: nil
)
And here is getSessionId :这是getSessionId :
@objc func getSessionId(_ notification: Notification)
{
if let data = notification.userInfo as? [String: String]
{
self.session_Id = data["uid"]
}
}
I tried a lot of workarounds like:我尝试了很多解决方法,例如:
1- Change name: NSNotification.Name("didReceiveSession") to name: NSNotification.Name(rawValue: "didReceiveSession") in both view controllers 1-在两个视图控制器中将名称:NSNotification.Name( "didReceiveSession")更改为名称:NSNotification.Name(rawValue:"didReceiveSession")
2- Create an instance in view-controller-A: 2- 在视图控制器-A 中创建一个实例:
2.1- share = view-controller-A() 2.1- share = view-controller-A()
2.2- Change object: nil to object: view-controller-A.share in the addObserver (view-controller-B) 2.2- 将object: nil更改为object: view-controller-A.share在 addObserver (view-controller-B)
3- Change the selector in view-controller-B from selector: #selector(self.getSessionId) to selector: #selector(getSessionId(_:)) 3- 将view-controller-B中的选择器从选择器:#selector(self.getSessionId)更改为选择器:#selector(getSessionId(_:))
But unfortunately, all attempts did not work.但不幸的是,所有尝试都没有奏效。 I can't figure out what's wrong.我不知道出了什么问题。
This is a very incorrect way to use Notifications.这是使用通知的一种非常不正确的方式。
What you want to do is pass your "sessionID" value to from VC-A
to VC-B
when VC-A
is moving to / presenting VC-B
.当VC-A
移动到/呈现 VC-B 时,您想要做的是将您的“sessionID”值从VC-A
传递到VC-B
VC-B
。
Assuming you are using a segue ...假设您使用的是segue ...
Based on what you posted, you already have a variable / property in VC-B
, probably:根据您发布的内容,您已经在VC-B
中有一个变量/属性,可能是:
var session_ID: [String] = ""
So, in VC-A
, you would do something like this:因此,在VC-A
中,您将执行以下操作:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let nextVC = segue.destination as? VCB {
nextVC.session_ID = self.uid
}
}
Your problem lies in the sequence of events.您的问题在于事件的顺序。
When post
is called before addObserver
, there is no place for the OS to deliver the notification to, and therefore the notification is discarded.当post
在addObserver
之前调用时,操作系统没有地方可以将通知传递到,因此通知被丢弃。
You need to make sure that you have an active observers, ie that addObserver
is called before you do your `post.你需要确保你有一个活跃的观察者,即addObserver
在你做你的`post之前被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.