2012-05-01 17 views
5

Những gì tôi muốn làm:ABPeoplePicker chọn nhiều và trở về mảng email

  1. hiện tại người sử dụng với tất cả các địa chỉ liên lạc trên điện thoại của mình rằng có một địa chỉ email trong liên lạc.
  2. Cho phép người dùng chọn/bỏ chọn bất kỳ số địa chỉ liên hệ nào trước khi nhấn "xong".
  3. Trả lại một mảng địa chỉ email ... hoặc một loạt các từ điển có tất cả thông tin liên hệ cho các địa chỉ liên hệ đã chọn.

Những gì tôi đã cố gắng:

ABPeoplePicker nhưng tôi không thể có được nó để làm việc với nhiều lựa chọn địa chỉ liên lạc.

Trả lời

3

Bạn có thể trở NO cho phương pháp đại biểu này:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
         kABPersonFirstNameProperty); 
    NSLog(@"Name %@", name); 
    // Do stuff with the person record 
    return NO; 
} 

Điều này cho phép người dùng lựa chọn một số người.

+0

Đó chính xác là những gì tôi cần. Tôi có thể thêm người dùng vào một 'NSSet' từ đó và đảm bảo rằng họ chỉ được thêm một lần. Một điều về điều này, không có dấu hiệu cho người dùng rằng bất cứ điều gì đã xảy ra ... là có một cách để thêm một "dấu kiểm" hoặc một cái gì đó bên cạnh hàng khi được chọn và loại bỏ nó nếu bỏ chọn? –

+0

Điều đó, tôi không chắc chắn. Tôi đã tự hỏi bản thân mình. Tôi nghĩ rằng bạn có thể truy cập vào bộ điều khiển xem bảng, nhưng nó dường như không được tiếp xúc. Bộ điều khiển bộ chọn người là một lớp con điều khiển điều hướng. Nếu bạn muốn làm cho nó rõ ràng hơn, nó có thể mất nhiều công sức hơn. –

+0

Tôi nghĩ rằng tôi có thể phải sử dụng 'ABAddressBookCopyArrayOfAllPeople' và sau đó thực hiện một TableView tùy chỉnh từ đó để làm những gì tôi muốn làm. Tôi sẽ cho bạn biết những gì tôi đưa ra. –

13

tôi đã có thể làm những gì tôi nghĩ rằng bạn đang mô tả với đoạn mã sau:

#import <UIKit/UIKit.h> 
#import <AddressBook/AddressBook.h> 

@interface ELEViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
@end 

@interface ELEViewController() 
@property (nonatomic, strong) NSArray *arrayOfPeople; 
@property (nonatomic, assign) CFArrayRef people; 
@property (nonatomic, strong) NSMutableSet *selectedPeople; 
@end 

@implementation ELEViewController 
@synthesize arrayOfPeople = _arrayOfPeople; 
@synthesize people = _people; 
@synthesize selectedPeople = _selectedPeople; 

- (NSMutableSet *) selectedPeople { 
    if (_selectedPeople == nil) { 
    _selectedPeople = [[NSMutableSet alloc] init]; 
    } 
    return _selectedPeople; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 50, 300, 300) 
                style:UITableViewStylePlain]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [self.view addSubview:tableView]; 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    self.people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    self.arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
    [tableView reloadData]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"ContactCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier]; 
    } 

    int index = indexPath.row; 
    ABRecordRef person = CFArrayGetValueAtIndex(self.people, index); 
    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
                  kABPersonFirstNameProperty); 
    NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, 
                    kABPersonLastNameProperty); 
    NSString *name = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

    cell.textLabel.text = name; 
    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 
    return self.arrayOfPeople.count; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    id person = [self.arrayOfPeople objectAtIndex:indexPath.row]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [self.selectedPeople addObject:person]; 
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [self.selectedPeople removeObject:person]; 
    } 
    NSLog(@"%@", self.selectedPeople); 
} 

@end 

Điều này cho thấy toàn bộ addressbook trong một tableView và sau đó chọn đặt một dấu kiểm bên cạnh sự tiếp xúc và thêm chúng vào một NSSet. Việc bỏ chọn sẽ xóa dấu kiểm và loại bỏ mục nhập khỏi NSSet. Tôi thêm toàn bộ ABPerson vào NSSet, do đó bạn sẽ vẫn phải sử dụng API C để làm việc với nó sau này.

+0

Bạn không chắc chắn nếu nó trả lời câu hỏi OPs - nhưng nó đã giúp tôi ra ngoài. Cảm ơn. – firecall

+0

Cảm ơn bạn đã viết mã. Cần thiết để thực hiện điều này mà không cần ctrlr nav, lưu tôi một vài phút. –

+0

Cảm ơn. Làm cách nào bạn xử lý việc loại bỏ bộ điều khiển? – barfoon

Các vấn đề liên quan