0

私のRubyMotionアプリではaccess_token、すべてのUIViewControllerインスタンスでアクセス可能なと呼ばれる属性が必要です。

私のコントローラーはすべて、TableControllerまたはのサブクラスですAppController

attr_accessorforTableControllerとを作成してみましたAppControllerが、問題は、新しい値の割り当てが、TableControllerまたはAppController同時に設定されないことです。

どうすればこれを達成できますか?

4

2 に答える 2

2

TableControllerクラスのサブクラスAppControllerController作成し、その中に属性を追加します。

于 2012-12-12T15:10:24.253 に答える
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

実際には、トークンを含むプロパティとトークンに加えてプロパティを保持するクラスを作成します。

于 2012-12-12T19:16:28.140 に答える