2012-05-01 1 views
5

の配列を返す:ABPeoplePickerは多くを選択し、私が何をしたいのか、メール

  1. は接触している電子メールアドレスを持って自分のデバイス上のすべての連絡先をユーザに提示します。
  2. 「完了」を押す前に、任意の数の連絡先を選択/選択解除できるようにします。
  3. 電子メールアドレスの配列...または選択した連絡先のすべての連絡先情報を含む辞書の配列を返します。私が試してみました何

は:

ABPeoplePickerが、私はそれは複数の連絡先を選択して動作するように取得することはできませんよ。

答えて

3

あなたはこのデリゲートメソッドのためNOを返すことができます。

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

これにより、ユーザは、人々の数を選択することができます。

+0

これは私が必要としていたものです。私はそこから 'NSSet'にユーザを追加し、それらが一度だけ追加されていることを確認することができます。これについての1つのことは、何も起こっていないことをユーザーに知らせるものではありません。選択されたときに行の横に「チェックマーク」や何かを追加し、選択を解除すると削除する方法はありますか? –

+0

それはわかりません。私は自分自身が不思議だった。私はあなたがテーブルビューコントローラにアクセスできるかもしれないと思ったが、露出されていないようだ。ユーザー選択コントローラはナビゲーションコントローラのサブクラスです。より明白にしたい場合は、より多くの作業が必要になります。 –

+0

私は 'ABAddressBookCopyArrayOfAllPeople'を使ってから、自分がしたいことをするためにカスタムTableViewを作成しなければならないかもしれないと思っています。私はあなたが私が何を思いついているかを知らせます。 –

13

私は私はあなたが次のコードで記述していると思うものを行うことができました:

#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 

これはのtableViewで全体のアドレス帳を示し、その後、次の連絡先へプットにチェックマークを選択し、それらを追加しますNSSet。選択を解除すると、チェックマークが削除され、NSSetからエントリが削除されます。私はABPersonをNSSetに追加するので、後でC APIを使用して作業する必要があります。

+0

OPの質問に答えたかどうかは分かりませんが、私を出す。ありがとう。 – firecall

+0

コードありがとうございます。 nav ctrlrを使わずにこれを実装する必要があり、数分節約できました。 –

+0

ありがとうございます。あなたはどのようにコントローラの解消を扱いますか? – barfoon