在Teacup的Tweet示例之后,我试图访问布局块之外的一个变量
class WindowController < TeacupWindowController
stylesheet :pref_window
layout do
@loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts @loginButton.class
end
puts @loginButton.class第一个but返回NSButton类,而第二个but返回Nil类。
如果需要以编程方式对@loginButton进行更改,如何访问@loginButton?
例如:
@loginButton.setState(NSOnState)不在布局块之外工作。
发布于 2013-12-10 23:41:16
您可以在attr_accessor上使用WindowController,然后在布局块中可以使用self.loginButton,它将在WindowController上分配给您访问按钮的权限。
我还假设第二个puts实际上在另一个方法中,这只是示例代码。
class WindowController < TeacupWindowController
stylesheet :pref_window
attr_accessor :loginButton
layout do
self.loginButton = subview(
NSButton, :loginButton,
state: NSOffState,
buttonType: NSSwitchButton,
action: 'login',
target: self,
)
puts self.loginButton.class
end
puts self.loginButton.class
endhttps://stackoverflow.com/questions/20505192
复制相似问题