0

Context We were developing an ios app that can uses opencv and had to change our viewcontrollers to .mm

opencv related functions in the .mm won't execute code that involves changes in the ui. GazeTracker is an NSObject that tells the state of the user's gaze and it works fine

we thought of using observers so that we'll use a selector in the viewController called stateChanged which will execute whenever the state in gazeTracker is changed.

"stateChanged" is never called. We initially thought it was just gazeTracker so we replaced it with "self" (meaning the viewController) and it still won't work. Our understanding of the "observer" is that when a value in an object is changed, the selector is called. However we don't know the purpose of "object" in "addObserver:selector:name:object".

the original code

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:gazeTracker.state
selector:@selector(stateChanged) 
name:@"stateChanged" 
object:nil];

}
-(void)stateChanged{
NSLog(@"some value in gaze tracker has changed");
}

with "self"

 - (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stateChanged) 
name:@"stateChanged" 
object:nil];

}
-(void)stateChanged{
NSLog(@"some value in gaze tracker has changed");
}
Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29
  • I think you are getting confused between notification and KVO. In your code you are registering for a notification with name `stateChanged`. This notification must be posted by some entity in your code on required state change. In KVO you can register for changes in particular properties of an instance. – Amar May 30 '13 at 09:58
  • @Amar Uhm, thanks but we don't understand what you just said. We are very new to xcode. We just want a method to be called when a change occurs in the value of an object. The work around we did just now is to use a thread in the viewcontroller that checks if there is any change, then calls a method in the viewcontroller that will do something when the change happens. but we think there is a better way to do this. we tried to read the documentation in the ios dev site but we didn't understand it either. – Gandalf The White May 31 '13 at 02:52

0 Answers0