Tag: nsnotificationcenter

用于位置更新多个视图的单身人士

我想在任何视图中按下导航栏中的定位button之后,在所有视图中更新用户位置。 我已经开始创build一个单身人士。 Location.h #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface Location : NSObject <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager* locationManager; + (Location*)sharedSingleton; @end Location.m #import "Location.h" @implementation Location { CLLocationManager *locationManager; CLGeocoder *geocoder; CLPlacemark *placemark; } @synthesize locationManager; – (id)init { self = [super init]; if(self) { self.locationManager = [CLLocationManager new]; [self.locationManager setDelegate:self]; [self.locationManager setDistanceFilter:kCLDistanceFilterNone]; [self.locationManager […]

为什么你必须删除ios8中的观察者?

阅读这篇文章的iOS 9后,我知道你不需要removeObserver了。 但是对于iOS 8,您需要在viewController的deinit方法中删除Observer。 但我无法理解它。 如果一个viewController被释放,那么它的DEAD不是? 为什么我们需要做一个removeObserver。 这是一个观察者,就像打电话给一个永远不会接电话的死人一样 我不了解什么?

为什么EAAccessoryDidConnectNotification发生两次?

我有一个class级pipe理来自iPad的外部附件的信息。 在init中我有以下代码: – (id) init { self = [super init]; if (!self) return; [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; //we want to hear about accessories connecting and disconnecting [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil]; … } 在dealloc我有 – (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil]; [[EAAccessoryManager […]

当使用ARC的NSNotificationCenter代码块方法时,不会调用视图控制器dealloc

当我在视图控制器的-viewDidLoad:方法中使用-addObserverForName: object: queue: usingBlock: for NSNotificationCenter时, -dealloc方法最终不会被调用。 (当我删除-addObserverForName: object: queue: usingBlock: ,再次调用-dealloc 。) 使用-addObserver: selector: name: object:似乎没有这个问题。 我究竟做错了什么? (我的项目正在使用ARC。) 下面是我的实现的一个例子,以防万一我在这里做错了什么: [[NSNotificationCenter defaultCenter] addObserverForName:@"Update result" object:nil queue:nil usingBlock:^(NSNotification *note) { updateResult = YES; }]; 在此先感谢您的帮助。 我已经尝试添加以下(无济于事): – (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([self isMovingFromParentViewController]) { [[NSNotificationCenter defaultCenter] removeObserver:self]; } }

KVO与NSNotifications

使用KVO有没有什么好处,而不是NSNotification的更“通用”(和我的opion更强大)function?

检测UILabel文本中的更改

是否有可能为UILabel的文本属性更改时设置通知? 我试图用UITextFields时,我找不到一个UILabel,但它没有工作。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(posttosocial) name:UITextFieldTextDidChangeNotification object:nowplaying];

如何检查当前是否显示UIViewController?

如何检查当前是否显示UIViewController ? 我的UIViewControllers正在监听NSNotifications – 即使他们没有显示(即没有显示)。 所以我可以有10个UIViewController在后台观察来自NSNotificationCenter 。 当一个NSNotification被UIViewController发布和接收,我想知道它是否正在显示。 如果不是的话,我只需要设置一个布尔值,这样就可以在视图呈现的时候处理它。 如果它现在正在显示,我会做更多的事情,如立即更新表,等等…

NSNotification removeObserver问题

我要么是脑部受损,要么是缺乏NSNotificationCenter 问题是,如果我创build一个观察者,并在下一行将尝试删除它像这样: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllVisibleMapViews) name:@"ClearVisibleMaps" object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"ClearVisibleMaps"]; 我明白了 *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MyApp 0x592db70> for the key path "ClearVisibleMaps" from <NSNotificationCenter 0x4e0fbb0> because it is not registered as an observer.' 我添加和删除观察员线后,只是为了说明一个观点。 在我的代码中,我将在dealloc中使用remove。 那么,为什么它告诉我,我没有添加和观察者在一开始呢?

如何编写NSNotification的unit testing

我在swift中工作,我想刷新一个页面,所以我使用通知发送它,我在一个ViewController发布通知,并添加观察者在另一个,它是完美的工作。 我想要做的就是快速添加unit testing。 我查了很多网站,但无法做到这一点。 我是新来的迅速,不知道从哪里开始。 基本上工作是,当我点击button通知张贴和当下一个视图控制器加载通知观察员被添加。 我怎么做unit testing 提前致谢 编辑:代码 NSNotificationCenter.defaultCenter().postNotificationName("notificationName", object: nil) 并添加观察者 NSNotificationCenter.defaultCenter().addObserver(self, selector: "vvv:",name:"notificationName", object: nil)

由NSNotificationCenter给出的键盘大小

我想从UISearchBar调用的键盘上添加一个accessoryView。 由于UISearchBar没有实现这个属性,我刚刚创build了一个工具条。 继苹果关于此事的文档之后,我决定使用通知中心不仅要知道何时调用键盘,还要知道键盘的大小,这取决于方向。 我已经按照文档上的例子,并在keyboardWasShown方法,我调用一个animation,将显示在键盘上的工具栏。 像这样的东西: -(void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary *info=[aNotification userInfo]; CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; NSLog(@"width: %.1f; heigth: %.1f", keyboardSize.width, keyboardSize.height ); [self showAccessoryView:keyboardSize.height]; } 而在animation上,我这样设置工具栏的框架: self.auxiliaryKeyboardBar.frame=CGRectMake(0, self.view.frame.size.height-(44+kbh), self.view.frame.size.width, 44); 其中44是工具栏的静态高度,kbh是从上面的方法传递的keyboard.size.heigth。 我观察到的问题是由userInfo Dictionary给出的键盘大小总是被引用到纵向方向。 所以,NSLog的纵向定位是: width: 320.0; heigth: 216.0 width: 320.0; heigth: 216.0 ,没关系 但是当我改变方向为风景,我打电话给键盘,NSLog如下: width: 162.0; heigth: 480.0 width: 162.0; heigth: 480.0 ,这使得工具栏超出了范围。 所以,我最终在调用animation之前添加了一个条件,例如: if ([self […]