nsnotificationcenter

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

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:

Detecting Network Connectivity Changes using Reachability, NSNotification and Network Link Conditioner in Swift

早过忘川 提交于 2019-11-26 17:17:29
I'm trying to integrate network connectivity detection into my app, however it seems that somewhere along the line I have made a mistake as my network changes are not being detected/printed out into the console. As mentioned in the post, I'm currently using these following classes and tools for the job: Reachability {.h, .m} NSNotificationCenter Network Link Conditioner Code In the AppDelegate.Swift , I've set up the NSNotificationCenter to detect changes: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // ... // A:

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 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

iOS 11 - Keyboard Height is returning 0 in keyboard notification

孤者浪人 提交于 2019-11-26 12:38:49
问题 I have been using Keyboard notifications without any problem and getting exact height of Keyboard. - (void)keyboardDidShow:(NSNotification *) notification{ CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; NSLog(@\"%f\",keyboardSize.height);} but with iOS 11 the size of keyboard is 0 when the notification is called. What is the problem occurring in this scenario? I am using xcode 9 Beta 5 回答1: Use this: CGSize keyboardSize = [[

How to pass object with NSNotificationCenter

╄→尐↘猪︶ㄣ 提交于 2019-11-26 08:44:55
问题 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

OSX: Detect system-wide keyDown events?

[亡魂溺海] 提交于 2019-11-26 07:56:34
问题 I\'m working on an typing-tutor application for Mac OSX that needs to have keystrokes forwarded to it, even when the application is not in focus. Is there a way to have the system forward keystrokes to the app, possibly through NSDistributedNotificationCenter? I\'ve googled myself silly, and haven\'t been able to find an answer... EDIT: Sample code below . Thanks @NSGod for pointing me in the right direction -- I ended up adding a global events monitor using the method

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

泪湿孤枕 提交于 2019-11-26 03:49:13
问题 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

How to receive NSNotifications from UIWebView embedded YouTube video playback

限于喜欢 提交于 2019-11-26 02:16:58
问题 I didn\'t received any notifications for MPMoviePlayerController . What am I doing wrong? I use following logic. I\'m begining to play youtube video in UIWebView . UIWebView calls a standard MPMoviePlayerController . I don\'t control MPMoviePlayerController because I didn\'t instantiate MPMoviePlayerController . I run youtube\'s clip with autoplay (1 second delay): [self performSelector:@selector(touchInView:) withObject:b afterDelay:1]; My code is: - (void)viewDidLoad { [super viewDidLoad];