当您想在视图控制器之间共享数据时……解决方案是 NSNotificationCenter。
我知道这并不能完全回答问题,但是如果我想在视图控制器之间共享数据:在发送视图控制器中
NSMutableDictionary *toshare = [[NSMutabledictionary alloc] init];
[toshare setValue:valueToShare forKey:@"shared"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"imSharing" withObject:nil userInfo:(NSDictionary *)toshare];
在接收视图控制器中:放置在viewDidLoad中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imSharing:) name:@"imSharing" object:nil];
然后添加这个函数:
-(void)imSharing:(NSNotification *)notification {
NSDictionary *dictionary = [notification userInfo];
//do something with [dictionary objectForKey:@"shared"];
dictionary = nil;
}
这适用于在 2 个视图控制器之间共享数据......好吧,无论如何都是少量数据。
否则在此处查看有关在视图控制器之间共享数据的更多信息