Passing a dictionary between two viewcontrollers?

前端 未结 2 1307
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 07:10

I have an application in which I have a webservice call that returns data as a dictionary. I want to access this dictionary in another view controller for loading the values

2条回答
  •  盖世英雄少女心
    2021-01-17 07:19

    I am assuming that the webservice is called because something happened (button clicked, viewDidLoad/viewDidAppear). If this is the case, passing a reference of the UIViewController to the webservice class is a perfect valid option. Keep in mind that for this relationship you should create a protocol, so on your webservice class you have something like this:

    id referenceToViewController;
    

    This ViewControllerResponseProtocolwould define a method like this:

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

    So when the webservice class has build the NSDictionary you can the above method from the referenceToViewController:

    [referenceToViewController responseFromWebservice:myDictionary];
    

    If there isn't any kind of relationship between both, you use could NSNotificationCenter for it.

    P.S: The solution of skram is perfectly valid if you already have the NSDictionary from the webservice on the initial UIViewController and now you want to pass it to a new UIViewController. Although I don't think that's what you want.

提交回复
热议问题