nsnotifications

Objective C: Where to remove observer for NSNotification?

半城伤御伤魂 提交于 2019-11-27 03:02:16
I have an objective C class. In it, I created a init method and set up a NSNotification in it //Set up NSNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData) name:@"Answer Submitted" object:nil]; Where do I set the [[NSNotificationCenter defaultCenter] removeObserver:self] in this class? I know that for a UIViewController , I can add it into the viewDidUnload method So what needs to be done if I just created an objective c Class? The generic answer would be "as soon as you no longer need the notifications". This is obviously not a satisfying answer. I

Best way to update badgeValue of UITabBarController from a UIView

可紊 提交于 2019-11-27 02:23:03
问题 I have a tabBarController set up in the AppDelegate and have a few UIViewControllers with Nav Controllers. In one of the TabBar items, after I have pushed a few UIViews I want to update the badgeValue item of a different TabBar item. Whats the best way to do this? The only way I can really think is a NSNotification and a singleton storage for the value, but it seems a lot of work for something simple, that and I have no idea about NSNotifications. I had a wild guess at something like super

How to pass object with NSNotificationCenter

被刻印的时光 ゝ 提交于 2019-11-26 23:36:24
I am trying to pass an object from my app delegate to a notification receiver in another class. I want to pass integer messageTotal . Right now I have: In Receiver: - (void) receiveTestNotification:(NSNotification *) notification { if ([[notification name] isEqualToString:@"TestNotification"]) NSLog (@"Successfully received the test notification!"); } - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self

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

£可爱£侵袭症+ 提交于 2019-11-26 20:26:13
问题 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

NSNotificationCenter vs delegation( using protocols )?

时光毁灭记忆、已成空白 提交于 2019-11-26 19:31:08
What are the pros and cons of each of them? Where should I use them specifically? The rule of thumb here is how many clients would like to be notified of an event. If it's mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model. If the event you emit may be of an interest to many objects at once (e.g. screen rotated, memory usage, user login/logout), then you should use the NSNotificationCenter . Their purposes are different: Notification is used to broadcast messages to possibly several recipients

How do you create custom notifications in Swift 3?

半世苍凉 提交于 2019-11-26 18:55:42
问题 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. 回答1: 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:

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

北城余情 提交于 2019-11-26 16:14:57
问题 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. 回答1: Send a notification: [[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self]; Receive it: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];

How to stop the Observer in NSNotification to called twice?

守給你的承諾、 提交于 2019-11-26 15:51:39
问题 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]; } 回答1: Solution 1: The first thing is to check if the notification itself is

How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

强颜欢笑 提交于 2019-11-26 15:46:49
I'm implementing socket.io in my swift ios app. Currently on several panels I'm listening to the server and wait for incoming messages. I'm doing so by calling the getChatMessage function in each panel: func getChatMessage(){ SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in dispatch_async(dispatch_get_main_queue(), { () -> Void in //do sth depending on which panel user is }) } } However I noticed it's a wrong approach and I need to change it - now I want to start listening for incoming messages only once and when any message comes - pass this message to any panel that

Objective C: Where to remove observer for NSNotification?

人盡茶涼 提交于 2019-11-26 10:20:10
问题 I have an objective C class. In it, I created a init method and set up a NSNotification in it //Set up NSNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData) name:@\"Answer Submitted\" object:nil]; Where do I set the [[NSNotificationCenter defaultCenter] removeObserver:self] in this class? I know that for a UIViewController , I can add it into the viewDidUnload method So what needs to be done if I just created an objective c Class? 回答1: The generic