Objective c @选择器-如何引用类方法?
我正在以编程方式添加菜单项:Objective c @选择器-如何引用类方法?,objective-c,cocoa,Objective C,Cocoa,我正在以编程方式添加菜单项: refreshItem=[[NSMenuItem alloc]initWithTitle:@“刷新”操作:@selector(refreshMenu)keyEquivalent:@“” 如果refreshMenu方法是实例方法,则称为fine,但我想将其转换为类方法: +(无效)刷新菜单{} 那么我该如何引用它呢?您仍然会使用@selector(刷新菜单),但是您会将NSMenuItem的-target设置为类(即,[self Class]),而不是实例(self)
refreshItem=[[NSMenuItem alloc]initWithTitle:@“刷新”操作:@selector(refreshMenu)keyEquivalent:@“”代码>
如果refreshMenu方法是实例方法,则称为fine,但我想将其转换为类方法:
+(无效)刷新菜单{}
那么我该如何引用它呢?您仍然会使用@selector(刷新菜单)
,但是您会将NSMenuItem
的-target
设置为类(即,[self Class]
),而不是实例(self
)。您仍然会使用@selector(刷新菜单)
,但是您应该将-NSMenuItem
的-target
设置为类(即[self Class]
),而不是实例(self
)。注意类本身就是对象:它们是元类的实例
[menuItem setTarget:[MyClass class]];
请注意,类本身就是对象:它们是元类的实例
[menuItem setTarget:[MyClass class]];