2012-12-12 1 views
0

내 RubyMotion 앱에서 모든 UIViewController 인스턴스에서 액세스 할 수있는 access_token이라는 속성을 갖고 싶습니다.UIViewController 인스턴스에서 전역으로 액세스 할 수있는 속성

내 모든 컨트롤러는 TableController 또는 AppController의 하위 클래스입니다. 문제는 새로운 값을 할당하는 것은 동시에 또는 TableControllerAppController 설정할 수 없다는 것이다 비록

난, TableControllerAppController위한 attr_accessor를 만드는 시도.

어떻게하면됩니까?

답변

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 

현실에서, 내가 포함하고 토큰에 추가 특성을 보유하는 클래스를 만들 것이다 :

나는이 비슷한 있습니다.

+0

이것은 멋지지만 여전히 한 가지 문제가 있습니다. 'AppController' 서브 클래스'UIViewController'와'TableController' 서브 클래스'UITableViewController' ... –

+1

UITableViewController는 UIViewController를 상속받습니다. – aceofspades

2

Controller 클래스의 TableControllerAppController 서브 클래스를 만들고 그 안에 속성을 추가하십시오.

+0

이것은 매우 간단합니다. 그렇게 천재적이기 때문에 이런 식으로 생각하지 않았을 것입니다. –