简体   繁体   中英

Converting a strong reference to weak reference to self

How do you convert this to a weak reference to self?

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyViewController.handleMessage(_:)), name: "NewMessage", object: nil)

NSNotificationCenter.defaultCenter().removeObserver(self, name: "NewMessage", object: nil)

Also, a related question--when does the view controller actually get destroyed? I'm assuming when you tab in and out of different views, view controllers still stay alive right? ie They don't get destroyed on viewDidDisappear.

this is the function in viewcontroller where it actually get out of the momory so here you can remove observer

                deinit {
                    // you code

                }

and for making weak reference of self you can use

                weak var this = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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