Passing variables through classes in Objective-C

我的未来我决定 提交于 2020-01-06 10:12:20

问题


In my scenario I have 2 view controllers, one tied to the main view and one that is connected to the first one as a subview.

Now let's say that my App Delegate class wants to pass a string to the subview controller. What is the best practice to achieve this? If I wanted to pass it to the 1st controller i could just say something like

[self.firstController getStringInCustomFunction:[NSString stringWithFormat:@"200%d", 9]];

Also keep in mind that this call might have to be asynchronous.

Coming from ActionScript, normally I would just add and event listener and move my variables though events. What's the equivalent in objective-c?


回答1:


Coming from ActionScript, normally I would just add and event listener and move my variables though events. What's the equivalent in objective-c?

Take a look at NSNotificationCenter.

Specifically, postNotificationName:object:userInfo, wherein you create an NSNotification that includes an NSDictionary of objects you pass inside userInfo.

On the other end, you have another object that is registered to "hear" an NSNotification of a specific name. That other object calls whatever method is specified in the registration. You might unpackage the userInfo dictionary in this method, to retrieve the object of interest.



来源:https://stackoverflow.com/questions/771895/passing-variables-through-classes-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!