xcode 将带有 NSNotificationCenter 的对象传递给其他视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8233416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:31:51  来源:igfitidea点击:

Pass object with NSNotificationCenter to other view

iphonexcodensnotificationsnsnotificationcenter

提问by Jorge Najera T

I am trying to pass an object from my main view class to other notification receiver in another class.

我试图将一个对象从我的主视图类传递给另一个类中的其他通知接收器。

I want to pass an object named country, that loads all the cities from an SOAP Request in the Main Controller and i want to send it to my next view.

我想传递一个名为 country 的对象,该对象从主控制器中的 SOAP 请求加载所有城市,我想将其发送到我的下一个视图。

country = [[Country alloc] init];

country = [[国家分配] init];

Country header:

国家标题:

@interface Country : NSObject
{
    NSString *name;
    NSMutableArray *cities;
}

@property (nonatomic,retain) NSString *name;

- (void)addCity:(Cities *)city;
- (NSArray *)getCities;
- (int)citiesCount;    
@end

I found a way to pass data with NSNotificatios is using a NSDictionary in UserInfo. But its not possible to send the whole object instead of converting to an NSDictionary? Or what's the best way to transfer it? Im stuck trying to figure out how to pass the objects.

我发现一种通过 NSNotificatios 传递数据的方法是在 UserInfo 中使用 NSDictionary。但是不可能发送整个对象而不是转换为 NSDictionary?或者什么是最好的转移方式?我一直试图弄清楚如何传递对象。

Actually i got working this simple NSNotification on my App.

实际上,我在我的应用程序上使用了这个简单的 NSNotification。

NSNotification in the Main View Controller implementation:

主视图控制器实现中的 NSNotification:

//---Call the next View---
DetailViewController *detail = [self.storyboardinstantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES]; 

//--Transfer Data to2View 
[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil];

NSNotification in 2View Controller implementation:

2View 控制器实现中的 NSNotification:

 // Check if MSG is RECEIVE
- (void)checkMSG:(NSNotification *)note {

    NSLog(@"Received Notification");
}

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(checkMSG:) 
                                                 name:@"citiesListComplete" object:nil];

回答by ColdLogic

Oooooo, so close. I have a feeling you do not understand what an NSDictionaryis though.

呜呜呜,好近。我有一种感觉,你不明白 anNSDictionary是什么。

Post your notification with this:

发布您的通知:

Country *country = [[[Country alloc] init] autorelease];
//Populate the country object however you want

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:country forKey:@"Country"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil userInfo:dictionary];

then get the country object like this:

然后像这样获取国家对象:

- (void)checkMSG:(NSNotification *)note {

    Country *country = [[note userInfo] valueForKey:@"Country"];

    NSLog(@"Received Notification - Country = %@", country);
}

You don't need to convert your object into a NSDictionary. Instead, you need to send a NSDictionarywith your object. This allows you to send lots of information, all based on keys in the NSDictionary, with your NSNotification.

您不需要将对象转换为NSDictionary. 相反,您需要NSDictionary随对象一起发送一个。这使您可以发送大量的信息,所有基于键NSDictionary,您的NSNotification

回答by 2ank3th

For SwiftYou can pass dictionary with using the below code

对于 Swift,您可以使用以下代码传递字典

NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?, userInfo aUserInfo: [NSObject : AnyObject]?)

for example

例如

NSNotificationCenter.defaultCenter().postNotificationName("OrderCancelled", object: nil, userInfo: ["success":true])

And read this dictionary from

并从

 func updated(notification: NSNotification){

         notification.userInfo?["success"] as! Bool 
    }