When a cocoa method wants a selector as a parameter, how do I express this in ruby?

喜你入骨 提交于 2019-12-24 20:27:04

问题


In this tutorial for sheet programming in cocoa, I am told to invoke the following method:

[[alert beginSheetModalForWindow:[searchField window] 
    modalDelegate:self 
    didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) 
    contextInfo:nil];

I have written this as follows in ruby,

alert.beginSheetModalForWindow(self.window, 
    modalDelegate:self,
    didEndSelector: :alertDidEnd,
    contextInfo:nil)

Of course, the didEndSelector part is wrong. Later in my code I have a method alertDidEnd, which takes returnCode and contextInfo as arguments. When I looked at self.methods I noticed that the method is listed as alertDidEnd:returnCode:contextInfo:. In the sample code above an '@' is used to mark the selector. This is accomplished in Macruby with a symbol, but in this case the symbol would contain colons, which is not allowed. How should I represent this method name as a symbol? I wasn't able to find this information on my own, where should I have looked that I didn't?

Thanks!


回答1:


As noted in the MacRuby docs, symbols are bridged with selectors. So you'd do:

alert.beginSheetModalForWindow(self.window, 
    modalDelegate:self,
    didEndSelector: :'alertDidEnd:returnCode:contextInfo:',
    contextInfo:nil)



回答2:


Have you tried using a Symbol? It seems to work in RubyCocoa.



来源:https://stackoverflow.com/questions/6740374/when-a-cocoa-method-wants-a-selector-as-a-parameter-how-do-i-express-this-in-ru

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