Cocoa:ABPeople PickerView - 如何单击一个人来触发方法?

时间:2013-01-01 09:10:15

标签: cocoa interface action abpeoplepickerview outlet

我到处寻找,希望也许有人可以指出我正确的方向。

我只是想在每次用户选择不同的记录时运行一个方法。

更大的图片(如果有另一种方式)是当用户选择记录(单击)时,人员电话号码将被放入分段控件。

我试过了:

  1. 要将操作连接到按钮,我通常会打开助理编辑器,然后右键单击拖动到.h文件。但是当我使用这个abpeoplepickerview时,我只得到一个Outlet连接类型?

2 个答案:

答案 0 :(得分:0)

人们选择器是一个。 “复合视图”实际上包括一个tableview,2个按钮和一个搜索字段(IIRC)

回答:
你运气不好,这个组件不适合你但当然你做了一些黑客攻击:

- (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]);
} 

答案 1 :(得分:0)

ABPeoplePickerView可以准确地提供您需要的通知。看看课程参考的结尾。

@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
}

如果您关闭窗口,请不要忘记删除观察者。