1

I've looked everywhere, and hope that perhaps someone can point me in the right direction.

I just want to run a method each time the user selects a different record.

The bigger picture (in case there is an alternate way) is that when the user selects the record (single click) the persons phone numbers are to be put into a segmented control.

I've tried:

  1. To connect an action to a button, I usually open the assistant editor, and right-click drag to the .h file. But when I'm doing it with this abpeoplepickerview I only get an Outlet connection type?
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202

2 Answers2

0

the people picker is a . 'compound view' that actually consits of a tableview, 2 buttons and a searchfield (IIRC)

answer:
you're out of luck and this component isnt suitable for you BUT of course you do some hacking:

- (void)viewDidLoad {
    //you get the internal tableview
    id views = [self findSubviewsOfKind:NSClassFromString(@"ABPeoplePickerTableView") withTag:NSNotFound inView:sef.peoplePickerView];
    id view = [views count] ? [views objectAtIndex:0] : nil;

    //subscribe to the notification
    if([view respondsToSelector:@selector(selectedRow)]) {
        [[NSNotificationCenter defaultCenter] addObserverForName:NSTableViewSelectionDidChangeNotification object:view queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            [self peoplePickerSelectedRecordChanged:self.peoplePickerView];
        }];
    }
}

- (NSArray *)findSubviewsOfKind:(Class)kind withTag:(NSInteger)tag inView:(NSView*)v {
NSMutableArray *array = [NSMutableArray array];
    if(kind==nil || [v isKindOfClass:kind]) {
        if(tag==NSNotFound || v.tag==tag) {
            [array addObject:v];
        }
    }

    for (id subview in v.subviews) {
        NSArray *vChild = [self findSubviewsOfKind:kind withTag:tag inView:subview];
        [array addObjectsFromArray:vChild];
    }

    return array;
}

- (IBAction)peoplePickerSelectedRecordChanged:(id)sender {
    NSLog(@"%@", [sender selectedRecords]);
} 
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Thank you for your answer, however i'm not sure what i'm to do with the code you have been so kind as to provide. I suppose i'm a n00b to obj-c, and need to be told everything to do. As at the moment, I can't see why this code would work, and therefore have no idea how to use it. I know what i need (just a void method triggered each time the user clicks a different name)... Thanks if your able to further help me... – Dai Lafing Jan 02 '13 at 05:55
  • Ok ill add some explanation and your beloved void method tonight :)) – Daij-Djan Jan 02 '13 at 07:00
  • edited the answer a bit to make it trivial to use. 'peoplePickerSelectedRecordChanged' is fired when an entry is selected – Daij-Djan Jan 02 '13 at 14:46
0

ABPeoplePickerView gives notifications for exactly what you need. Look near the end of the class reference.

@implementation someController
@synthesize picker;  //your ABPeoplePickerView

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// or some other method that gets called early on
{
  [[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(notificate:)
              name:ABPeoplePickerNameSelectionDidChangeNotification
              object:picker];
}

 - (void) notificate: (NSNotification*) notification {
  ABPerson *person = picker.selectedRecords.firstObject;
  NSLog(@"NOTIFIED %@"), person.name); 
  // name is a property of ABPerson I added in a category
  // do what you will
}

Don't forget to remove the observer if you dismiss the window.

AlexT
  • 596
  • 7
  • 15