将具有NSNotificationCenter的对象传递给其他视图 [英] Pass object with NSNotificationCenter to other view

查看:98
本文介绍了将具有NSNotificationCenter的对象传递给其他视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 = [[Country alloc] init];

country = [[Country alloc] init];

国家标题:

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

@property (nonatomic,retain) NSString *name;

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

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

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.

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

Actually i got working this simple NSNotification on my App.

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

NSNotification in the Main View Controller implementation:

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

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

2View Controller实施中的NSNotification:

NSNotification in 2View Controller implementation:

 // 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];


推荐答案

Oooooo,如此接近。我有一种感觉,你不明白 NSDictionary 是什么。

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

发布你的通知:

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);
}

您无需将对象转换为的NSDictionary 。相反,您需要使用您的对象发送 NSDictionary 。这允许您使用 NSNotification 发送大量基于 NSDictionary 中的键的信息。

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

这篇关于将具有NSNotificationCenter的对象传递给其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆