2016-09-14 53 views
0

我想傳遞接口對象作爲通知參數。傳遞接口對象作爲通知對象

我有一個像

@interface StudentDetails : NSObject 
{ 
NSMutableArray* studentList; 
int   grade; 
} 
@end 

的接口,我想通過StudentDetails的對象作爲通知參數如下圖所示:

StudentDetails* pInterfaceCommand1 = [[StudentDetails alloc] init]; 
// student list is initialize to 1,2 & grade is 10 
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:pInterfaceCommand1 forKey:@"aKey"]; 

    [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil userInfo:userInfo deliverImmediately:YES]; 

但我不能夠接收其他類的通知。 但是,當我張貼通知如下圖所示,我能夠接受的值爲1

NSDictionary* userInfo = @{@"data": @1}; 
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil userInfo:userInfo deliverImmediately:YES]; 

請讓我知道需要什麼樣的變化。

+2

您沒有收到通知,或者您沒有收到有效負載? – Avi

+0

嗨。 1.未收到以下情況的通知 StudentDetails * pInterfaceCommand1 = [[StudentDetails alloc] init]; NSDictionary * userInfo = [NSDictionary dictionaryWithObject:pInterfaceCommand1 forKey:@「aKey」]; [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@「testNotification」object:nil userInfo:userInfo deliverImmediately:YES]; 2.接收回應 NSDictionary * userInfo = @ {@「data」:@ 1}; 。 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@「testNotification」object:nil userInfo:userInfo deliverImmediately:YES]; – Phillip

+0

可以'StudentDetails'被序列化嗎? – Willeke

回答

0

您的代碼使用的是NSDistributedNotificationCenter,這是不尋常的。你真的想把通知發送到不同的過程嗎?如果需要接收通知的對象在同一個程序中,則通常使用NSNotificationCenter

+0

嗨。 謝謝你的回覆。 我需要NSDistributedNotificationCenter,因爲應用程序正在向不同的進程發送通知 – Phillip