简体   繁体   English

如何在iOS类中注册通知中心

[英]How to register Notification Center in class iOS

I am new to ios programming, I wanted to register notification center in the class, not in the view controller, I want to send some action from one view controller this custom class receives that action and it performs some view controller navigation.I wanted to have custom notification center in ios. 我是ios编程的新手,我想在该类中而不是在视图控制器中注册通知中心,我想从一个视图控制器发送一些操作,此自定义类接收该操作并执行一些视图控制器导航。在ios中有自定义通知中心。

code: 码:

NotificationCenter.default.post(name: Notification.Name(rawValue: "key"), object: nil)
class MainReceiver: NotificationCenter
{
    override func post(name aName: NSNotification.Name, object anObject: Any?)
    {
        print("coming here")
    }

}

Here you have snipped code: 在这里,您已摘录代码:

class MainReceiver {

    init() {

        NotificationCenter.default.addObserver(self, selector: #selector(handleMethod(_:)), name: NSNotification.Name(rawValue: "Notification_Name"), object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func handleMethod(_ notification: Notification) {
        // handle
    }
}

From Apple's NSNotificationCenter documentation : Apple的NSNotificationCenter文档中

Each running Cocoa program has a default notification center. 每个正在运行的Cocoa程序都有一个默认的通知中心。 You typically don't create your own. 您通常不创建自己的。

I'm not sure why you think you want to create your own notification center, but trust me, you don't. 我不确定您为什么认为自己想创建自己的通知中心,但是请相信我,但是您不愿意。


However, you can easily subscribe an object (an instance of a custom class, like you say you want to use) to notifications: 但是,您可以轻松地将对象(自定义类的实例,就像您说的那样使用)订阅通知:

// let object = Whatever()
NotificationCenter.default.addObserver(object, selector: #selector(didReceive(notification:)), name: NSNotification.Name(rawValue: "key"), object: nil)
// object's didReceive(notification:) function will now be called when a notification with the specified name is posted. This can be posted from anywhere.

A while back, the function had to be decorated with the @objc attribute. @objc ,该函数必须使用@objc属性进行修饰。 I'm not sure if that's still the case, but if so, you'd use @objc func didReceive(notification: Notification) as your declaration in the receiving class. 我不确定是否仍然如此,但是如果是这样,则可以在接收类中使用@objc func didReceive(notification: Notification)作为声明。

If I understand your question correctly I think what you are looking for is the blocked based variant for the NotificationCenter. 如果我正确理解了您的问题,我认为您正在寻找的是NotificationCenter基于阻止的变体。

You can find it in the official docs . 您可以在官方文档中找到它。

Example code from the docs: 来自文档的示例代码:

let center = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()
self.localeChangeObserver = center.addObserverForName(NSCurrentLocaleDidChangeNotification, object: nil, queue: mainQueue) { (note) in
print("The user's locale changed to: \(NSLocale.currentLocale().localeIdentifier)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM