nsnotificationcenter

NotificationCenter registered selector is not called

♀尐吖头ヾ 提交于 2019-11-28 12:38:39
问题 I currently have this code below to see if the day has elapsed, but it doesn't seem to be working. Have I coded this correctly? NotificationCenter.default.addObserver(self, selector:"calendarDayDidChange:", name:NSNotification.Name.NSCalendarDayChanged, object:nil) func calendarDayDidChange(notification : NSNotification) { // code to respond to notification } 回答1: I think the way you registered observer is not correct. Please try the below and check. NotificationCenter.default.addObserver

NSNotification VS KVO

给你一囗甜甜゛ 提交于 2019-11-28 10:18:55
I feel that i don't fully understand difference between KVO and NSNotification... They seem to be so similar... Could you make some example showing when is best to use one method and when the other ? I don't speak about Bind and IB, but i mean add Observer programmatically in my code with NSNotificationCenter or KVO [self.preferenceController addObserver:self forKeyPath:@"color" options:NSKeyValueObservingOptionOld context:@"Color-change" ]; KVO only works on values, NSNotification can be used for value changes but it can be used for anything and can carry a much greater payload. For example,

NSNotificationCenter Swift 3.0 on keyboard show and hide

时光总嘲笑我的痴心妄想 提交于 2019-11-28 09:51:41
I am trying to run a function when the keyboard shows and disappears and have the following code: let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(ViewController.keyBoardUp(Notification :)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) And the function keyBoardUp below: func keyBoardUp( Notification: NSNotification){ print("HELLO") } However the function doesn't print to the console when the keyboard shows. Help would be greatly appreciated Swift 3: override func viewDidLoad() { super.viewDidLoad() NotificationCenter

iOS NSNotificationCenter to check whether the app came from background to foreground

最后都变了- 提交于 2019-11-28 01:49:00
I have a situation in which i have to intialize an object everytime when it comes from background to foreground and that should be using the NSNotificationCenter not with appdelegate because iam building a static library so there wont be appdelegate with that so please help me in the same. Have you tried UIApplicationWillEnterForegroundNotification ? The app also posts a UIApplicationWillEnterForegroundNotification notification shortly before calling applicationWillEnterForeground: to give interested objects a chance to respond to the transition. Subscribe to notification: [

Is there any way to “wait here…” in code - just like an empty loop?

早过忘川 提交于 2019-11-28 01:30:18
问题 Consider this code: [self otherStuff]; // "wait here..." until something finishes while(!self.someFlag){} [self moreStuff]; Note that this all happens ON THE SAME THREAD - we do not want to go to another thread. otherStuff could do things like connect to the cloud, get input from the user, etc. so it would take a lot of time and could follow many possible paths. otherStuff would set self.someFlag to true, when otherStuff is finally finished. This works perfectly and there's no problem with it

Swift 3 NSNotificationCenter Keyboardwillshow/hide

余生长醉 提交于 2019-11-28 00:58:06
I have a piece of code that worked in Swift 2 and I tried using Xcode to update the code to the newest version and I fixed everything except two issues. I have this code : let loginvc: LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification,

Detect Change in UILabel Text

核能气质少年 提交于 2019-11-28 00:11:35
问题 Is it possible to set a Notification for when a UILabel's text property is changed? I tried the one used for UITextFields when I couldn't find one for a UILabel, but it didn't work. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(posttosocial) name:UITextFieldTextDidChangeNotification object:nowplaying]; 回答1: You can use key-value observing (KVO): [label addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context

How to create a class to send and receive events through NSNotificationCenter in Objective-C?

不羁的心 提交于 2019-11-27 21:41:21
I need to create two classes and both should be able to send and receive the events through the NSNotificationCenter methods.ie Both should have the sendEvent and receiveEvent methods: @implementation Class A -(void)sendEvent { addObserver:--- name:---- object:--- } -(void)ReceiveEvent { postNotificationName: --- object:--- } @end Same as such another class say ClassB should also be able to send and receive the events. How can it be done? Ideally an object would start observing interesting events as soon as its initialized. So it will register all interesting events with the NotificationCenter

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