UIViewController实例中的全局可访问属性

时间:2012-12-12 15:04:15

标签: ruby rubymotion

在我的RubyMotion应用程序中,我想在每个UIViewController实例中都有一个名为access_token的属性。

我的所有控制器都是TableControllerAppController的子类。

我尝试为attr_accessorTableController创建AppController,但问题是不会为TableControllerAppController设置新值在同一时间。

我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:2)

制作TableController类的AppControllerController个子类,并在其中添加该属性。

答案 1 :(得分:1)

我个人使用类变量来做这样的事情,因为iOS应用程序不是多用户的。利用继承类共享类变量的事实,@@ access_token将为所有UIViewController子类(或者您自己的子类,如果您愿意)具有相同的值。

我有类似的东西:

# Reopen and extend
class UIViewController # Actually I prefer UIViewController.class_eval do
  @@access_token = nil # This will have the same value for all UIViewController children

  def self.access_token=(value)
    @@access_token = value
  end

  def self.access_token
    @@access_token
  end
end

实际上,我会构建一个包含属性的类,包括令牌。