Jor*_*a T 9 iphone xcode nsnotifications nsnotificationcenter
我试图将一个对象从我的主视图类传递到另一个类中的其他通知接收器.
我想传递一个名为country的对象,它从主控制器中的SOAP请求加载所有城市,我想将它发送到我的下一个视图.
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
Run Code Online (Sandbox Code Playgroud)
我发现使用NSNotificatios传递数据的方法是在UserInfo中使用NSDictionary.但它不可能发送整个对象而不是转换为NSDictionary?或者转移它的最佳方式是什么?我试图弄清楚如何传递物体.
实际上我在我的应用程序上运行了这个简单的NSNotification.
主视图控制器实现中的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];
Run Code Online (Sandbox Code Playgroud)
2View Controller实现中的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];
Run Code Online (Sandbox Code Playgroud)
Col*_*gic 25
哦,如此接近.我有一种感觉,你不明白它NSDictionary是什么.
发布您的通知:
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];
Run Code Online (Sandbox Code Playgroud)
然后得到这样的国家对象:
- (void)checkMSG:(NSNotification *)note {
Country *country = [[note userInfo] valueForKey:@"Country"];
NSLog(@"Received Notification - Country = %@", country);
}
Run Code Online (Sandbox Code Playgroud)
您不需要将对象转换为NSDictionary.相反,您需要发送一个NSDictionary包含您的对象.这允许您发送大量信息,所有信息都基于NSDictionary您的密钥NSNotification.
| 归档时间: |
|
| 查看次数: |
15729 次 |
| 最近记录: |