2012-03-15 55 views
3

我想在我的應用程序委託中爲我的UIViewController中的屬性添加觀察者。KVO爲另一個類的財產

是否有可能像下面的代碼?

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]           ; 


    [self addObserver:self 
      forKeyPath:appDelegate.currentLocation 
       options:0 
       context:nil]; 

問題它期望「forKeyPath」的值爲NSString。

回答

6
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]          ; 

[appDelegate addObserver:self 
       forKeyPath:@"currentLocation" 
       options:0 
       context:nil];