Tag: nsnotificationcenter

iPhone的通知结果在“无法识别的select器发送到实例…”

为了简短NSNotification ,我在ClassA注册了下面的NSNotification监听器(在viewDidLoad ): [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong) name:@"playNotification" object:nil]; 我在ClassA.h声明了select器: – (void)playSong:(NSNotification *) notification; 并执行如下: – (void)playSong:(NSNotification *) notification { NSString *theTitle = [notification object]; NSLog(@"Play stuff", theTitle); } 在ClassB (在tableView:didSelectRowAtIndexPath:方法)我有: NSInteger row = [indexPath row]; NSString *stuff = [playlistArray objectAtIndex:row]; [[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff]; 这一切都结束了一个错误消息说: “发送给实例的无法识别的select器” 在playSong方法被调用之前。 有人可以帮我吗? 当从一个控制器发送通知到另一个时,我忘记了什么?

实例被解除分配,而主要的价值观察者仍然注册

我有一个UITableView。 在这里,我得到了不同的细胞。 每个单元格都有一个模型。 使用KVO和NotificationCenter,单元会监听模型的更改。 当我离开ViewController时,我得到这个错误: An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSKeyValueObservationInfo 0x109429cc0> ( […]

removeObserver不起作用

我有下一个代码: @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:@"Error" message:@"Some problem with server" delegate:self cancelButtonTitle:@"try again" otherButtonTitles:nil]; [alertView show]; } else if […]

NSNotificationCenter事件是同步接收还是asynchronous接收?

如果某个类注册了某个types的NSNotificationCenter事件,并且另一个类发布了该types的事件,则接收器中的代码将在发布类继续之前(同步)还是之后(asynchronous)执行? – (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"); } 在上面的代码示例中,将“来自呼叫者的Hello”之前或之后打印“来自接收者的Hello”?

在UIViewController中添加和删除观察者到NSNotificationCenter

看看各种苹果的例子(例如添加音乐 ),我看到他们将观察者添加到viewDidLoad的默认NSNotificationCenter ,然后在dealloc删除它们。 这看起来很危险,因为viewDidLoad可以被调用多次而不需要调用dealloc 。 然后这将多次添加相同的观察者,导致处理程序被多次调用。 解决这个问题的办法也是去除viewDidUnload中的观察者,但是这意味着同样的观察者可能会在dealloc被第二次移除,这似乎是一个潜在的问题。 我错过了什么?

如何在Swift中使用NSTimer和NSNotificationCentre来更新UITableViewCells

注意:请在Swift中请求答案。 我在做什么: 桌面单元格每1秒更新一次并显示实时倒计时。 我目前如何做: 我有一个包含标签的单元格的tableview。 当单元格生成时,它们调用一个函数来计算今天和已存储的目标date之间的时间,并显示标签中剩余的倒计时时间。 为了让单元格每秒更新标签,我使用了一个NSTimer ,每秒重新载入tableview。 问题:倒计时是有效的,但是当我尝试做一个Swipe-to-Delete操作时,由于NSTimer重新载入了tableview,它重置了swiped单元,所以不会再被刷新,当然这对于最终用户是不可接受的。 我正在尝试/潜在的解决scheme 我听说有一个更好的方法是使用由NSTimer触发的NSNotificationCenter ,并为每个单元添加侦听器。 每隔1秒从NSNotificationCenter发送“广播”,小区将接收“广播”并更新标签。 我试图添加一个监听器到代码中生成一个新单元的部分中的每个单元格: // Generate the cells override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("Tablecell", forIndexPath: indexPath) as UITableViewCell let countdownEntry = fetchedResultController.objectAtIndexPath(indexPath) as CountdownEntry // MARK: addObserver to "listen" for broadcasts NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateCountdownLabel", name: mySpecialNotificationKey, object: […]

iOS / iPhone可达性 – 如何使用Reachability.m / .h检查互联网何时丢失/无法访问

目前我正在使用苹果reachability.m / .h类,它的工作原理,除了它通知我的任何改变,因为我只想通知用户,如果networking不可达。 目前,如果我有一个互联网连接,然后松动networking它告诉我。 但是,当你重新连接到networking,它也告诉我,我不想要。 我只想告诉我什么时候有一个损失/没有networking。 我相信这跟电话有关: – (void)viewWillAppear:(BOOL)animated { // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; internetReachable = [[Reachability reachabilityForInternetConnection] retain]; [internetReachable startNotifier]; // check if a pathway to a random host exists hostReachable = [[Reachability reachabilityWithHostName: @"www.google.ca"] retain]; [hostReachable startNotifier]; // now patiently wait for the notification } 当调用-[NSNotificationCenter […]

在多个视图控制器中为一个观察者使用NSNotificationCenterselect器

我可以在多个视图控制器中使用select器getUpdate: 我注册我的LevelViewController作为WinViewController和WinViewController观察员。 后者2视图控制器都有一个后退button(当按下时,它会弹回到LevelVC ),通知的想法是告诉LevelVC是否更新集合视图单元格(通过viewWillAppear:方法)何时后退button被按下。 在viewWillAppear: ,我不想调用两个独立的方法(一个来自GameVC ,另一个来自WinVC ),以获得我的更新,我只是一个可以使用的stream体方法。 这是我打算(在LevelVC ): – (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUpdate:) name:@"getUpdateForCell" object:nil]; } 并纳入 – (void)getUpdate:(NSNotification *)notification { NSDictionary *data = [notification userInfo]; // pop } 两次…一次在GameVC ,另一次在GameVC中。 这可能吗? 或者我应该只做两个单独的通知?

iOS NSNotificationCenter来检查应用程序是否从后台到前台

我有一个情况,我必须每次初始化一个对象,当它从背景到前景,并应该使用NSNotificationCenter不与appdelegate,因为我build立一个静态库,所以不会被appdelegate所以请帮助我在相同的。

为什么我得到这个代码的“wait_fences:未能收到答复”?

为什么我得到这个代码的“wait_fences:未能收到答复”? 这是我使用通知回传给主线程的方式吗? #import "ViewController.h" @implementation ViewController @synthesize alert; #pragma mark – Background Thread Test Methods – (void) ConfigTasksForBackground:(id)sender{ NSLog(@"ConfigTasksForBackground – Starting"); [NSThread sleepForTimeInterval:6]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object:self]; NSLog(@"ConfigTasksForBackground – Ending"); } #pragma mark – Callbacks – (void) ModelChangedHandler:(NSNotification *) notification { if ([[notification name] isEqualToString:@"ModelChanged"]) { NSLog(@"ModelChangedHandler"); [self.alert dismissWithClickedButtonIndex:0 animated:false]; } } #pragma mark – […]