I have one method that add selected method for any annotation in my map but I don't want this method working for my annotation location (user location annotation) and I don't know how do it?
this is my code that add select method to any annotation and I want that this method don't working for my user location annotation.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSString *action = (__bridge NSString *)context;
if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) {
BOOL annotationSelected = [[change valueForKey:#"new"] boolValue];
if (annotationSelected) {
NSLog(#"Annotation was selected, do whatever required");
}else {
NSLog(#"Annotation was deselected, do what you must");
}
}
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *anAnnotationView in views) {
if (!Change) {
[anAnnotationView setCanShowCallout:NO]; //this method is for show popover annotation
[anAnnotationView addObserver:self forKeyPath:#"selected" options:NSKeyValueObservingOptionNew context:(__bridge void *)(ANNOTATION_SELECTED_DESELECTED)];
NSLog(#"%#",anAnnotationView);
}
}
}
Related
I am trying to make an observer to notify me whenever my laptop it automatically changes its wifi connection.
I have a "NetworkProperties" class where I store my connection properties:
#interface NetworkProperties : NSObject
{
#public
CWInterface *wfi;
#private
CWWiFiClient *wfc;
NSString *SSID;
NSString *BSSID;
NSString *phyMode;
NSString *hwAddr;
NSString *securityType;
}
#end
and a "GUIHandle" class where I try to handle my GUI. I also have the following functions to enable/disable the scaning:
#interface GUIHandle : NSObject
{
NetworkProperties *NP;
}
-(IBAction)startScan:(id)sender;
-(IBAction)stopScan:(id)sender;
#end
-(IBAction)startScan:(id)sender
{
done = 0;
NP = [[NetworkProperties alloc] init];
[NP scanNetworkProperties];
[NP->wfi addObserver:self forKeyPath:#"bssid" options:NSKeyValueObservingOptionNew context:nil];
}
-(IBAction)stopScan:(id)sender
{
[NP->wfi removeObserver:self forKeyPath:#"bssid"];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(#"We are here!\n");
if ([keyPath isEqual:#"bssid"]) {
NSLog(#"Value changed!\n");
}
}
However I do not get any notification if the "bssid" of my connection is changing. What am I doing wrong?
I'm not familiar with iOS but I'm trying to find when the default, built-in camera application is focusing. To do this I create my own separate Objective-C application and following this answer here [iPhone : camera autofocus observer? but I'm not getting anything from observeValueForKeyPath in the NSLog.
#import "ViewController.h"
#import "AVFoundation/AVCaptureDevice.h"
#import "AVFoundation/AVMediaFormat.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"viewDidLoad");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// callback
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(#"observeValueForKeyPath");
if( [keyPath isEqualToString:#"adjustingFocus"] ){
BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
NSLog(#"Is adjusting focus? %#", adjustingFocus ? #"YES" : #"NO" );
NSLog(#"Change dictionary: %#", change);
}
if( [keyPath isEqualToString:#"focusMode"] ){
AVCaptureFocusMode focusMode = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
NSLog(#"focusMode? %ld", focusMode);
}
}
// register observer
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear: animated];
NSLog(#"viewWillAppear");
AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
int flags = NSKeyValueObservingOptionNew;
[camDevice addObserver:self forKeyPath:#"adjustingFocus" options:flags context:nil];
[camDevice addObserver:self forKeyPath:#"focusMode" options:flags context:nil];
}
#end
Any help much appreciated.
For anyone who visits this question, the answer is what Bluewings wrote as a comment. I was trying to use KVO to observe one application from another which is not possible since only one lock on a capture device is possible at one time.
RCT_EXPORT_MODULE()
- (UIView *)view
{
return [[RNNativeListview alloc] initWithBridge:self.bridge];
}
RCT_EXPORT_VIEW_PROPERTY(rowHeight, float)
RCT_EXPORT_VIEW_PROPERTY(numRows, NSInteger)
I want to reload my UITableView whenever js updates numRows. How do I listen for this?
I don't think that KVO is a good solution.
You can just override setter for numRows property:
- (void)setNumRows:(NSInteger)numRows {
_numRows = numRows;
[self.tableView reloadData];
}
Or you can use RCT_CUSTOM_VIEW_PROPERTY:
RCT_CUSTOM_VIEW_PROPERTY(numRows, NSInteger, RNNativeListview) {
view.numRows = [RCTConvert NSInteger:json];
[view.tableView reloadData];
}
I solved it using an observer.
- (instancetype)initWithBridge:(RCTBridge *)bridge {
[self addObserver:self forKeyPath:#"self.numRows" options:NSKeyValueObservingOptionNew context:nil];
return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[self.tableView reloadData];
}
i have a singleton class with a nsmutablearray
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(#"Received notification: keyPath:%# ofObject:%# change:%#",
keyPath, object, change);
//[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
//array accessors
- (void)insertNewObject:(id)object
{
[self willChangeValueForKey:#"heldJoins"];
[heldJoins addObject:object];
[self didChangeValueForKey:#"heldJoins"];
}
- (void)removeObject:(id)object
{
[self willChangeValueForKey:#"heldJoins"];
[heldJoins removeObject:object];
[self didChangeValueForKey:#"heldJoins"];
}
i "observe" the changes made to this array from another class
my rootviewcontroller
[CCV addObserver:self forKeyPath:#"heldJoins" options:NSKeyValueChangeOldKey||NSKeyValueChangeNewKey context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"Received notification: keyPath:%# ofObject:%# change:%#",keyPath, object, [change allKeys]);
}
this works and i am notified when there is an object removed or added
however i cannot figure out (and maybe its not possible) how to see the object that was removed or added (mainly need the object when its removed not added though)
i know the NSDictionary variable Change should have my object but all it has is one key "kind" which always equals 1 since there is always 1 change made to the array.
the nsmutablearray is filled with numbers 500,100,300
so when one of those numbers is removed i would like to know which number was removed in my observer class
how can i do that?
answer code:
[CCV addObserver:self forKeyPath:#"heldJoins" options: NSKeyValueObservingOptionOld ||NSKeyValueChangeNewKey context:NULL];
NSLog(#"Received notification: keyPath:%# ofObject:%# change:%#",keyPath, object, [change valueForKey:#"new"]);
It sounds like you didn't specify NSKeyValueObservingOptionOld when you added the observer.
I have now nearly figured out how to Filter a NSTreeController, to do this I have sub-classed NSManagedObject and added some code to my App Delegate, I have also bound my NSSearchField to the filterPredicate of my App Delegate but I think I need to connect my NSTreeController and NSSearchField in some way to make it work.
Below I have posted all the code I have used so far to try and make it work.
NSManagedObject Sub-Class Header File.
#interface Managed_Object_Sub_Class : NSManagedObject {
NSArray *filteredChildren; // this should fix the compiler error
}
- (NSArray *)filteredChildren;
#end
NSManagedObject Sub-Class Implementation File.
#implementation Managed_Object_Sub_Class
static char *FilteredChildrenObservationContext;
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
if (self = [super initWithEntity:entity insertIntoManagedObjectContext:context]) {
[[NSApp delegate] addObserver:self forKeyPath:#"filterPredicate" options:0 context:&FilteredChildrenObservationContext];
[self addObserver:self forKeyPath:#"subGroup" options:0 context:&FilteredChildrenObservationContext];
}
return self;
}
// use finalize with GC
- (void)dealloc {
[[NSApp delegate] removeObserver:self forKeyPath:#"filterPredicate"];
[self removeObserver:self forKeyPath:#"subGroup"];
[super dealloc];
}
- (NSArray *)filteredChildren {
if (filteredChildren == nil) {
NSPredicate *predicate = [[NSApp delegate] filterPredicate];
if (predicate)
filteredChildren = [[[self valueForKey:#"subGroup"] filteredArrayUsingPredicate:predicate] copy];
else
filteredChildren = [[self valueForKey:#"subGroup"] copy];
}
return filteredChildren;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &FilteredChildrenObservationContext) {
[self willChangeValueForKey:#"filteredChildren"];
[filteredChildren release];
filteredChildren = nil;
[self didChangeValueForKey:#"filteredChildren"];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#end
Code Added To App Delegate Header File
NSPredicate *filterPredicate;
Code Added To App Delegate Implementation File
- (NSPredicate *)filterPredicate {
return filterPredicate;
}
- (void)setFilterPredicate:(NSPredicate *)newFilterPredicate {
if (filterPredicate != newFilterPredicate) {
[filterPredicate release];
filterPredicate = [newFilterPredicate retain];
}
}
Search Field Binding
alt text http://snapplr.com/snap/vs9q
This doesn't work yet, and so that is why I am asking what I need to do from here to make it work, like I said I think I need to connect the NSSearchField and NSTreeController Together in some way.
Again I hav answered my own question, I also hope that this will help other people so they know how to Filter a NSTreeController.
To make it work from my post above do the following.
1.For your entity set the Class as your NSManagedObject Sub-Class in my Case JGManagedObject.
alt text http://dvlp.me/c3k
2.For your search field in IB set the predicate format to what you want to Filter ( The Property in your entity, for me it is name).
alt text http://dvlp.me/9k9rw