0

Following the Tweets example of Teacup, I'm trying to access a variable outside of the layout block

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

the first puts returns NSButton class, but the second one returns Nil class.

how can I access @loginButton if I need to programmatically make changes to it?

For example:

@loginButton.setState(NSOnState)

doesn't work outside the layout block.

vash
  • 52
  • 1
  • 6

1 Answers1

1

You can use an attr_accessor on the WindowController, then in the layout block you can use self.loginButton and it will be assigned on the WindowController giving you access to the button.

I'm also assuming the second puts is actually in another method and this is just example code.

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
end
FluffyJack
  • 1,732
  • 10
  • 15
  • thanks for the hint. In the tweets example i linked in the question, they don't use self. – vash Dec 11 '13 at 11:09
  • That's because they use the variable only inside the action: related to the variable. So I can use the @loginButton variable inside my `def login` but not in other methods definition. Is this right? – vash Dec 11 '13 at 11:16
  • I think I may have gotten this wrong... what your problem might be is simply that the layout block hasn't been called yet. Everything should be fine if you're using @loginButton in an action or something else that would be run after the view has been loaded and displayed. – FluffyJack Dec 12 '13 at 00:23