在两个视图控制器之间传递字典?

我有一个应用程序,我有一个web服务调用返回数据作为字典。 我想在另一个视图控制器中访问这个字典,以将值加载到表中。

任何人都可以演示如何将这个响应字典从一个视图控制器传递给另一个?

你可以在你的AnotherViewController定义一个NSDictionary属性,并从前一个控制器中进行设置。 下面我将举一个简短的例子。

 //.h @interface AnotherViewController { NSDictionary *data; } @property(nonatomic,retain) NSDictionary *data; @end //.m @implementation AnotherViewController @synthesize data; 

现在从当前的控制器,在初始化AnotherViewController您可以在显示它之前设置字典。

 AnotherViewController *controller = [[AnotherViewController alloc] init]; controller.data = myCurrentDictionary; 

现在AnotherViewController具有一个data属性和前一个控制器的值。

希望这可以帮助。

我假设webservice被调用,因为发生了什么(button点击, viewDidLoad / viewDidAppear )。 如果是这种情况,将UIViewController的引用传递给webservice类是一个完美的有效选项。 请记住,对于这种关系,你应该创build一个协议,所以在你的web服务类你有这样的东西:

 id<ViewControllerResponseProtocol> referenceToViewController; 

这个ViewControllerResponseProtocol会定义一个像这样的方法:

 -(void)responseFromWebservice:(NSDictionary*)myDictionary; 

所以当webservice类构buildNSDictionary您可以从referenceToViewController获得上述方法:

 [referenceToViewController responseFromWebservice:myDictionary]; 

如果两者之间没有任何关系,则可以使用NSNotificationCenter

PS :skram的解决scheme是完全有效的,如果你已经在最初的UIViewController上有web服务的NSDictionary ,现在你想把它传递给一个新的UIViewController 。 虽然我不认为这是你想要的。