问题
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