gpt4 book ai didi

ios - 从 objectivec 发送通知并 swift 接收

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:55 26 4
gpt4 key购买 nike

尝试从 objective-c 发送通知并快速接收。我没有看到来自发送者或接收者的错误,并且没有在目标 View Controller 中调用函数

ViewController 1: objective-c

    [[NSNotificationCenter defaultCenter]
postNotificationName:@"notifyme"
object:self
userInfo:self.profile];

View Controller 2:快速

override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)

}

func receiveNotification(ns: NSNotification){
print("Received Notification")

}

最佳答案

在Swift3中,我解决了这个问题

发送OC

[[NSNotificationCenter defaultCenter] postNotificationName:"You NotificationName" object:nil];

接收 Swift 3

func applicationDidBecomeActive(_ application: UIApplication) {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotification), name: NSNotification.Name.init(rawValue: "You NotificationName"), object: nil)
}
func handleNotification() -> Void {

}

关于ios - 从 objectivec 发送通知并 swift 接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com