我试图将一个对象从我的主视图类传递到另一个类中的其他通知接收器。
我想传递一个名为 Country 的对象,该对象从主控制器中的 SOAP 请求加载所有城市,并且我想将其发送到下一个视图。
国家 = [[国家分配] 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 吗?或者说转移的最佳方式是什么?我一直在思考如何传递这些物体。
实际上我在我的应用程序上工作了这个简单的 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];
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];