nsnotifications

Where to remove observer for NSNotification in Swift?

守給你的承諾、 提交于 2019-11-28 04:27:14
Where should I remove the observer for NSNotification in Swift, since viewDidUnload and dealloc() are unavailable? Use below method which functions same as dealloc . deinit { // Release all resources // perform the deinitialization } A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how intializers are written with the init keyword. Deinitializers are only available on class types. Swift Deinitializer As of iOS 9 (and OS X 10.11), you don't need to remove observers yourself, if you're not using block based

Why doesn't Remove Observer from NSNotificationCenter:addObserverForName:usingBlock get called

混江龙づ霸主 提交于 2019-11-27 20:44:22
I'm confused on why the observer is never removed in the following code. In my viewDidAppear I have the following: -(void)viewDidAppear:(BOOL)animated{ id gpsObserver = [[NSNotificationCenter defaultCenter] addObserverForName:FI_NOTES[kNotificationsGPSUpdated] object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ NSLog(@"run once, and only once!"); [[NSNotificationCenter defaultCenter] removeObserver:gpsObserver]; }]; } The observer never gets removed and the statement is output every time the notification is sent out. Can anyone provide any guidance? When the block

is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread)

↘锁芯ラ 提交于 2019-11-27 20:34:31
问题 Is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread). That is, there are are there any gottcha's for this purpose? Background want to call back to main UI thread from a background thread (e.g. performSelectorInBackground) could use performSelectorOnMainThread to communicate back, but wondering if it is OK to use a notification? For example [[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object

How do you create custom notifications in Swift 3?

人盡茶涼 提交于 2019-11-27 17:19:10
In Objective-C, a custom notification is just a plain NSString, but it's not obvious in the WWDC version of Swift 3 just what it should be. You could also use a protocol for this protocol NotificationName { var name: Notification.Name { get } } extension RawRepresentable where RawValue == String, Self: NotificationName { var name: Notification.Name { get { return Notification.Name(self.rawValue) } } } And then define your notification names as an enum anywhere you want. For example: class MyClass { enum Notifications: String, NotificationName { case myNotification } } And use it like

Why is my NSNotification its observer called multiple times?

大城市里の小女人 提交于 2019-11-27 14:27:46
问题 Within an App I make use of several viewcontrollers. On one viewcontroller an observer is initialized as follows: [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil]; Even when removing the NSNotification before initializing the number of executions of myMethod: is being summed up by the amount of repeated views on the respective

How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

巧了我就是萌 提交于 2019-11-27 13:11:21
Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to see a quick example. Send a notification: [[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self]; Receive it: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil]; Act on it: - (void)cacheUpdated:(NSNotification *)notification { [self load]; } And dispose of it: [

How to stop the Observer in NSNotification to called twice?

不羁岁月 提交于 2019-11-27 12:01:01
I have an observer of NSNotification which is called twice. I do not know what to do with it. I googled it but no solution found. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectedToServer:) name:@"ConnectedToServer" object:nil]; - (void)connectedToServer:(NSNotification*)notification { [[NSNotificationCenter defaultCenter] postNotificationName:@"SendMessageToServer" object:message]; } Solution 1: The first thing is to check if the notification itself is posted twice. Solution 2: Even if the notification is posted only once, the action will be called as many

KVO vs NSNotification vs protocol/delegates?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:35:46
问题 Though I have some idea which to use when but the exact usage is still not clear to me. Can someone explain with example...? Thanks. 回答1: Use a delegate if you want to talk to only one object. For example, a tableView has a delegate - only one object should be responsible for dealing with it. Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app that there has been a memory warning. Because lots

NSNotificationCenter trapping and tracing all NSNotifications

夙愿已清 提交于 2019-11-27 09:18:18
问题 For some better understanding on what happens “under the hood”, I would love to do a complete trace of any notifications happening within my application. Naïve as I am, the first thing I tried was registering like this: Somewhere in my app: { [...] [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(traceNotifications:) name:nil object:nil]; [...] } - (void)traceNotifications:(NSNotification *)notification { NSLog(@"received notification %@", [notification name]); } I do

Text change notification for an NSTextField

人走茶凉 提交于 2019-11-27 05:27:51
问题 I would like to use the code from the answer to this question: How to observe the value of an NSTextField on an NSTextField in order to observe changes on the string stored in the NSTextField. [[NSNotificationCenter defaultCenter] addObserverForName:NSTextViewDidChangeSelectionNotification object:self.textView queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){ NSLog(@"Text: %@", self.textView.textStorage.string); }]; The class used here is an NSTextView. I can't find a