通过Objective-C中的类传递变量 [英] Passing variables through classes in Objective-C
问题描述
在我的场景中,我有2个视图控制器,一个连接到主视图,一个连接到第一个视图。
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.
现在我们来说我的App Delegate类希望将一个字符串传递给子视图控制器。实现这一目标的最佳做法是什么?如果我想把它传递给第一个控制器,我可以这样说:
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.
来自ActionScript,通常我只需添加事件监听器,并通过事件移动变量。在Objective-C中相当于什么?
Coming from ActionScript, normally I would just add and event listener and move my variables though events. What's the equivalent in objective-c?
推荐答案
来自ActionScript,通常我只需添加事件侦听器并移动我的变量虽然事件。 Objective-C中的等价物是什么?
查看 NSNotificationCenter
。
具体来说, postNotificationName:object:userInfo
,其中创建 NSNotification
,其中包含一个 NSDictionary 您通过的对象 userInfo
。
另一方面,您有另一个对象是注册为听到NSNotification 的具体名称。该另一个对象调用注册中指定的任何方法。您可以在此方法中解压缩 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.
这篇关于通过Objective-C中的类传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!