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");
}