NSNotificationCenter addObserver in Swift while call a private method

前端 未结 2 1336
离开以前
离开以前 2021-02-19 19:15

I use the addObserver API to receive notification:

NSNotificationCenter.defaultCenter().addObserver(self, selector: \"methodOFReceivedNotication:\",         


        
2条回答
  •  生来不讨喜
    2021-02-19 20:09

    Have you considered using -addObserverForName:object:queue:usingBlock:?

    NSNotificationCenter.defaultCenter().addObserverForName("NotificationIdentifier", object: nil, queue: nil, usingBlock: {
        [unowned self] note in
        self.methodOFReceivedNotication(note)
    })
    

    or instead of calling the private method, just performing the action.

    NSNotificationCenter.defaultCenter().addObserverForName("NotificationIdentifier", object: nil, queue: nil, usingBlock: {
        [unowned self] note in
        // Action take on Notification
    })
    

提交回复
热议问题