我正在尝试将一个对象从我的主视图类传递到另一个类中的通知接收器。
我想传递一个名为country的对象,它在主控制器中通过SOAP请求加载所有城市,我想将其发送到下一个视图中。
country = [[Country alloc] init];
Country header:
@interface Country : NSObject
{
NSString *name;
NSMutableArray *cities;
}
@property (nonatomic,retain) NSString *name;
- (void)addCity:(Cities *)city;
- (NSArray *)getCities;
- (int)citiesCount;
@end
我发现使用UserInfo中的NSDictionary可以传递NSNotificatios数据。但是有没有可能直接发送整个对象而不是转换为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];
在两个视图控制器中实现的 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];