在我的RubyMotion应用程序中,我想在每个UIViewController实例中都有一个名为access_token
的属性。
我的所有控制器都是TableController
或AppController
的子类。
我尝试为attr_accessor
和TableController
创建AppController
,但问题是不会为TableController
或AppController
设置新值在同一时间。
我怎么能实现这个目标?
答案 0 :(得分:2)
制作TableController
类的AppController
和Controller
个子类,并在其中添加该属性。
答案 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
实际上,我会构建一个包含属性的类,包括令牌。