![](/img/trans.png)
[英]How to pass messages from one class to another through NSNotification
[英]How to pass userInfo in NSNotification?
我試圖使用NSNotification發送一些數據,但卡住了。 這是我的代碼:
// Posting Notification
NSDictionary *orientationData;
if(iFromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
orientationData = [NSDictionary dictionaryWithObject:@"Right"
forKey:@"Orientation"];
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:@"Abhinav"
object:nil
userInfo:orientationData];
// Adding observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:@"Abhinav"
object:nil];
現在如何在我的選擇器orientationChanged中獲取此userInfo字典?
您將獲得傳遞給您的函數的NSNotification對象。 這包括您提供給NSNotificationCenter的名稱,對象和用戶信息。
- (void)orientationChanged:(NSNotification *)notification
{
NSDictionary *dict = [notification userInfo];
}
您的選擇器必須具有:
接受參數。
例如
@selector(orientationChanged:)
然后在方法聲明中它可以接受NSNotification
參數。
您正在發布通知。 請修改通知觀察器,如下所示。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:@"Abhinav" object:nil];
- (void)orientationChanged:(NSNotification *)notification
{
NSDictionary *dict = [notification userInfo];
}
我希望,這個解決方案對你有用..
在swift中獲取userinfo對象
let dict = notification.userInfo
print(dict)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.