如何通过Cocoa中的委托访问全局实例/变量?(how to access global instance/variable via delegate in Cocoa?)

编程入门 行业动态 更新时间:2024-10-24 04:46:00
如何通过Cocoa中的委托访问全局实例/变量?(how to access global instance/variable via delegate in Cocoa?)

我更喜欢通过委托访问全局实例/变量。 (我不喜欢'extern NSInteger myInstance'模式)

这意味着所有全局实例/变量都在委托对象中。 在可可触摸中,我知道我可以使用

appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];

访问全局实例(myInstance),但我不确定它是否与Cocoa中的模式相同。

欢迎任何评论

谢谢

InterDev中

I prefer to access the global instance/variable via delegate. ( I do not like the mode as 'extern NSInteger myInstance')

This means that all global instances/variables are in a delegate object. In cocoa touch, I know I can use

appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];

to access the global instance(myInstance), but I am not sure if it is same as the mode in Cocoa.

Welcome any comment

Thanks

interdev

最满意答案

在Cooca中, NSApp是一个表示共享NSApplication实例的全局变量。 要获得代表,您可以简单地使用

id delegate = [NSApp delegate];

设置应用程序委托非常简单。 在MainMenu.xib , File's Owner是共享的NSApplication实例。 在MainMenu.xib实例化您的委托类(创建一个对象并将其类型设置为您的委托类),然后将File's Owner delegate出口拖到该实例。

In Cooca, NSApp is a global variable representing the shared NSApplication instance. To get the delegate, you can simply use

id delegate = [NSApp delegate];

Setting the app delegate is pretty simple. In MainMenu.xib, File's Owner is the shared NSApplication instance. Instantiate your delegate class in MainMenu.xib (create an object and set its type to your delegate class), then drag the File's Owner delegate outlet to that instance.

更多推荐

本文发布于:2023-08-06 04:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   变量   实例   Cocoa   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!