nsnotifications

Removing a NSNotificationCenter observer in iOS 5 ARC

帅比萌擦擦* 提交于 2019-11-29 11:05:10
问题 I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController . Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code: - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } As a test, I open the UIViewController (within

How do I access remote push notification data on applicationDidBecomeActive?

旧街凉风 提交于 2019-11-29 10:28:37
When receiving a remote push notification as the application is in the background, the app enters applicationDidBecomeActive. From there, how can I access the NSDictionary of data from the notification? The notification data is delivered to your app in application:didReceiveRemoteNotification: . If you want to process it in applicationDidBecomeActive: you should store it in application:didReceiveRemoteNotification: and read it again in applicationDidBecomeActive . Swift version: var dUserInfo: [NSObject : AnyObject]? func application(application: UIApplication, didFinishLaunchingWithOptions

Why is my NSNotification its observer called multiple times?

旧街凉风 提交于 2019-11-28 22:52:16
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 viewcontroller. Why does this happen and how can I avoid myMethod: being called more then once. Note: I

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

一笑奈何 提交于 2019-11-28 19:41:12
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:self]; Actually there is a gottcha; you'll crash randomly! That has been my experience. This has to do

KVO vs NSNotification vs protocol/delegates?

自古美人都是妖i 提交于 2019-11-28 15:54:19
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. 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 of objects in your app might want to lower their memory usage it's a notification. I don't think KVO is a

NSNotificationCenter trapping and tracing all NSNotifications

℡╲_俬逩灬. 提交于 2019-11-28 15:35:54
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 actually get a number of notifications that way. But at some point the application does crash. The

How should Finder Sync Extension and Main App communicate?

泪湿孤枕 提交于 2019-11-28 08:49:14
My use case: I have a 'MainApp' which does the syncing of files. I would like that 'MainApp' handles all server calls regarding syncing and other REST API calls such as document-sharing, etc. On the other hand, I would have a Finder Sync Extension which would show sync-status icon overlays. It would also have a file-context-menu-item 'Share' which would present a Share dialog where users can choose with whom to share the file. Questions: How should FinderSyncExtension and MainApp communicate? Should XCP be utilised and if so, is it ok that communication is two-ways? For example MainApp

Best way to update badgeValue of UITabBarController from a UIView

无人久伴 提交于 2019-11-28 08:33:18
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.tabBarController.otherView.tabBarItem.badgeValue = @"1" (as I set which tab is selected in a similar way)

Error with notification names while converting code to Swift 4.2

最后都变了- 提交于 2019-11-28 07:54:53
问题 The code below was working fine before Swift 4.2: NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) When I click the 'Fix' option, it becomes: NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil) But it is still marked an error. Here is the explanation: Type

Text change notification for an NSTextField

微笑、不失礼 提交于 2019-11-28 04:50:29
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 notification in NSTextField to use instead of NSTextViewDidChangeSelectionNotification. Is there a