如何通过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
传递给另一个类。
您必须使用“userInfo”变体并传递包含messageTotal整数的NSDictionary对象:
NSDictionary* userInfo = @{@"total": @(messageTotal)}; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc postNotificationName:@"eRXReceived" object:self userInfo:userInfo];
在接收端,您可以按如下方式访问userInfo字典:
-(void) receiveTestNotification:(NSNotification*)notification { if ([notification.name isEqualToString:@"TestNotification"]) { NSDictionary* userInfo = notification.userInfo; NSNumber* total = (NSNumber*)userInfo[@"total"]; NSLog (@"Successfully received test notification! %i", total.intValue); } }
build立在所提供的解决scheme,我认为这可能是有用的,以显示一个例子传递自己的自定义数据对象(我在这里引用作为“消息”作为每个问题)。
A类(发件人):
YourDataObject *message = [[YourDataObject alloc] init]; // set your message properties NSDictionary *dict = [NSDictionary dictionaryWithObject:message forKey:@"message"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:nil userInfo:dict];
B类(接收器):
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerAction:) name:@"NotificationMessageEvent" object:nil]; } #pragma mark - Notification -(void) triggerAction:(NSNotification *) notification { NSDictionary *dict = notification.userInfo; YourDataObject *message = [dict valueForKey:@"message"]; if (message != nil) { // do stuff here with your message data } }
Swift 2版本
正如@Johan Karlsson指出的那样…我做错了。 以下是使用NSNotificationCenter发送和接收信息的正确方法。
首先,我们看一下postNotificationName的初始值设定项:
init(name name: String, object object: AnyObject?, userInfo userInfo: [NSObject : AnyObject]?)
资源
我们将使用userInfo
parameter passing我们的信息。 Objective-C保留了[NSObject : AnyObject]
types。 因此,在Swift领域,我们所需要做的就是传入一个Swift字典,它包含NSObject
派生的键和可以是AnyObject
值。
有了这些知识,我们创build了一个字典,我们将传递给object
参数:
var userInfo = [String:String]() userInfo["UserName"] = "Dan" userInfo["Something"] = "Could be any object including a custom Type."
然后我们将字典传递给我们的对象参数。
寄件人
NSNotificationCenter.defaultCenter() .postNotificationName("myCustomId", object: nil, userInfo: userInfo)
接收器类
首先,我们需要确保我们的class级正在观察通知
override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("btnClicked:"), name: "myCustomId", object: nil) }
那我们可以收到我们的字典:
func btnClicked(notification: NSNotification) { let userInfo : [String:String!] = notification.userInfo as! [String:String!] let name = userInfo["UserName"] print(name) }
斯威夫特4
func post() { NotificationCenter.default.post(name: Notification.Name("SomeNotificationName"), object: nil, userInfo:["key0": "value", "key1": 1234]) } func addObservers() { NotificationCenter.default.addObserver(self, selector: #selector(someMethod), name: Notification.Name("SomeNotificationName"), object: nil) } @objc func someMethod(_ notification: Notification) { let info0 = notification.userInfo?["key0"] let info1 = notification.userInfo?["key1"] }
奖励:
用Notification.Name("SomeNotificationName")
replaceNotification.Name("SomeNotificationName")
extension Notification.Name { static let someNotificationName = Notification.Name("SomeNotificationName") }