Tag: nsnotificationcenter

如何使用postNotificationName:object传递NSDictionary:

我正在尝试使用NSNotificationCenter将UIView的NSDictionaryforms传递给UIViewController。 在发布通知时,字典工作正常,但在接收方法中,我无法访问字典中的任何对象。 这是我如何创build字典和张贴通知… itemDetails = [[NSDictionary alloc] initWithObjectsAndKeys:@"Topic 1", @"HelpTopic", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:itemDetails]; 在UIViewController我设置观察者… [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotSpotMore:) name:@"HotSpotTouched" object:nil]; 出于testing目的,hotSpotMore看起来像这样… – (void)hotSpotMore:(NSDictionary *)itemDetails{ NSLog(@"%@", itemDetails); NSLog(@"%@", [itemDetails objectForKey:@"HelpTopic"]); } 第一个NSLog工作正常显示字典的内容。 第二个日志引发以下exception… [NSConcreteNotification objectForKey:]: unrecognized selector sent to instance 0x712b130 我不明白为什么我不能访问传递的字典中的任何对象。 在此先感谢您的帮助。 约翰

我怎样才能听到发送到iOS NSNotificationCenter的默认中心的所有通知?

我想听所有派发到defaultCenter的通知。 公共和私人。 有谁知道我怎么能做到这一点?

iOS 11 – 键盘高度在键盘通知中返回0

我一直在使用键盘通知没有任何问题,并获得键盘的确切高度。 – (void)keyboardDidShow:(NSNotification *) notification{ CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; NSLog(@"%f",keyboardSize.height);} 但在iOS 11中,调用通知时键盘的大小为0。 在这种情况下发生了什么问题? 我正在使用xcode 9 Beta 5

在主线程上发布NSNotification

我发现下面的代码片断允许NSNotification从任何后台线程发布到主线程上。 我想知道这是否是一个安全和可接受的做法? dispatch_async(dispatch_get_main_queue(),^{ [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageRetrieved" object:nil userInfo:imageDict]; });

NSNotificationCenter的post导致“EXC_BAD_ACCESS”exception

UIViewController将自己添加到默认中心: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFood) name:@"editFood" object:nil]; 然后一个UITableView委托NSObject发布一个NSNotification : [[NSNotificationCenter defaultCenter] postNotificationName:@"editFood" object:self]; 在运行期间,它会得到一个EXC_BAD_ACCESSexception。 defaultCenter是否在某处被释放? 当我从UIViewController发布通知给UIViewController的时候,这个概念是可行的,但是这不应该有问题,对吧?

你如何在Swift 3中创build自定义通知?

在Objective-C中,自定义通知只是一个普通的NSString,但在Swift 3的WWDC版本中它并不明显。

如何使用NSNotificationcenter的对象属性

有人可以告诉我如何使用NSNotifcationCenter上的对象属性。 我想能够使用它来传递一个整数值给我的select器方法。 这是我如何在我的UI视图中设置通知侦听器。 看到我想要一个整数值被传递我不知道什么要取代零。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEvent:) name:@"myevent" object:nil]; – (void)receiveEvent:(NSNotification *)notification { // handle event NSLog(@"got event %@", notification); } 我从这样的另一个类发送通知。 该函数传递一个名为index的variables。 这是我想以某种方式发出通知的价值。 -(void) disptachFunction:(int) index { int pass= (int)index; [[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:pass]; //[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)aName#> object:<#(id)anObject#> }

Android相当于NSNotificationCenter

在将iPhone应用程序移植到Android的过程中,我正在寻找在应用程序内进行通信的最佳方式。 意图似乎是要走的路,这是最好的(唯一)的select? 在性能和编码方面,NSUserDefaults看起来比Intents更轻。 我也应该添加我有一个应用程序的子类的状态,但我需要使另一个活动意识到一个事件。

用Swift键盘移动视图

我有一个应用程序在视图的下半部分有一个文本字段。 这意味着,当我去键入文本字段的键盘覆盖文本字段。 如何在打字时向上移动视图,以便我可以看到正在键入的内容,然后在键盘消失时将其移回原始位置? 我到处寻找,但所有的解决scheme似乎是在Obj-C,我还不能完全转换。 任何帮助将不胜感激。

如何通过NSNotificationCenter传递对象

我试图从我的应用程序委托传递一个对象到另一个类的通知接收器。 我想传递整数messageTotal 。 现在我有: 在接收器中: – (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:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil]; 在正在进行通知的课程中: [UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal; [[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self]; 但是我想将对象messageTotal传递给另一个类。