View controllers sometimes do not receive an NSNotification

前端 未结 3 1567
情深已故
情深已故 2021-01-28 00:22

So, I am just testing NSNotifications on a variety of cases and this one is confusing. I would appreciate it if you could help me understand NSNotifications !

I have a N

3条回答
  •  [愿得一人]
    2021-01-28 01:14

    The differences you are describing seem to be due to changes in which objects are alive when. Views and view controllers do not exist indefinitely, and are not all created when the app starts. An object has to exist to receive and log a notification. The basic notification system is working as expected.

    You should be able to see the effect of lifetime on the messages received if you add log statements announcing when an object that is supposed to receive one of these notifications is created and when it is destroyed within the body of -init (or whatever the designated initializer of your superclass is) and -dealloc.

    Also: Your log statements will be easier to track down if you tag them with the function doing the logging like NSLog(@"%s: ", __func__). The compiler generates a string named __func__ for each function that contains the function's name.

提交回复
热议问题