nsnotificationcenter

removeObserver not working

*爱你&永不变心* 提交于 2019-12-03 09:06:34
I have next code: @implementation SplashViewVC - (void)viewDidLoad { [super viewDidLoad]; self.splashView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]]; self.activityIndicator.originY = 355.f; [[NSNotificationCenter defaultCenter] addObserverForName:NCDownloadComplete object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *n){ NSInteger errorCode = [n.userInfo[@"errorCode"] integerValue]; [self.activityIndicator stopAnimating]; if (errorCode == ERROR_CODE_NO_CONNECTION) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@

Adding and removing observers to NSNotificationCenter in a UIViewController

天涯浪子 提交于 2019-12-03 07:58:47
问题 Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad , then remove them in dealloc . This seems dangerous as viewDidLoad can be called multiple times without dealloc being called. This would then add the same observer multiple times, causing the handler to be called multiple times. A solution to this would be to also remove observers in viewDidUnload , but this would then mean the same observer could be

NSNotificationCenter removeObserver: in dealloc and thread-safety

≯℡__Kan透↙ 提交于 2019-12-03 07:52:43
问题 I'm using ARC and I'm calling [[NSNotificationCenter defaultCenter] removeObserver:someObserver]; in observer's dealloc . From NSNotificationCenter Class Reference Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated. NSNotificationCenter does not retain the observer. Q1: Is NSNotificationCenter thread-safe? In case, the observer is being deallocated(and removing observer from the

iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h

烈酒焚心 提交于 2019-12-03 05:17:57
问题 Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network. I believe it has something to do with the call: - (void)viewWillAppear:(BOOL)animated

Are NSNotificationCenter events received synchronously or asynchronously?

情到浓时终转凉″ 提交于 2019-12-03 02:04:04
If a class registers for NSNotificationCenter events of a certain type and another class posts an event of that type, will the code in the receiver execute before (synchronously) or after (asynchronously) the posting class continues? - (void)poster { [[NSNotificationCenter defaultCenter] postNotificationWithName:@"myevent" object:nil]; NSLog(@"Hello from poster"); } - (void)receiver { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector:(mySelector) name:@"myevent" object:nil]; } - (void) mySelector:(NSNotification *) notification { NSLog(@"Hello from receiver"); } In the

NSNotificationCenter removeObserver: in dealloc and thread-safety

纵然是瞬间 提交于 2019-12-02 22:59:45
I'm using ARC and I'm calling [[NSNotificationCenter defaultCenter] removeObserver:someObserver]; in observer's dealloc . From NSNotificationCenter Class Reference Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated. NSNotificationCenter does not retain the observer. Q1: Is NSNotificationCenter thread-safe? In case, the observer is being deallocated(and removing observer from the notification center) and another thread post a notification at the same time. I encounter random crash

Adding and removing observers to NSNotificationCenter in a UIViewController

拟墨画扇 提交于 2019-12-02 20:40:18
Looking at various Apple examples (for example Add Music ) in which I see they add observers to the default NSNotificationCenter in viewDidLoad , then remove them in dealloc . This seems dangerous as viewDidLoad can be called multiple times without dealloc being called. This would then add the same observer multiple times, causing the handler to be called multiple times. A solution to this would be to also remove observers in viewDidUnload , but this would then mean the same observer could be removed for a second time in dealloc which seems like a potential problem. What am I missing? Lorenzo

Refreshing Parent ViewController after dismissing ModalViewController

戏子无情 提交于 2019-12-02 19:35:22
In my iOS app, a user can select an image from a list, upon which they are presented with a modal that contains the image and options to delete the image. If the user chooses to delete the image, she is returned to the original viewController containing the list of images. I need to then refresh the original ViewController to take into account the deleted image. I tried using NSNotificationCenter to broadcast when an image is deleted to the parent View Controller. However, it seems like the broadcast is never received. Is there some other way to send data back to the parent ViewController

iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h

吃可爱长大的小学妹 提交于 2019-12-02 18:33:30
Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network. I believe it has something to do with the call: - (void)viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:

Swift NSNotificationCenter?

泄露秘密 提交于 2019-12-02 09:36:48
I'm trying to get the UITextViewTextDidChangeNotification to work. I'm new to using the NSNotificationCenter, so I'm having a hard time understanding what's going on exactly. I have a UITextView in a storyboard, and I've created an IBOutlet for it in my ViewController class and called it textView. This is my viewDidLoad function: override func viewDidLoad() { super.viewDidLoad() origin = self.view.frame.origin.y if let field = textView{ field.placeholder = placeholder field.layer.cornerRadius = 8 field.layer.borderWidth = 0.5 field.layer.borderColor = UIColor.grayColor().CGColor