使用委托给ABPeoplePickerNavigationController

时间:2012-08-06 21:21:26

标签: ios delegates uitabbarcontroller

我需要在UITabBarController中管理一个ABPeoplePickerNavigationController(我不想以模态方式显示ABPeoplePickerNavigationController,因为我想保持tabbar可见)。然后我使用此代码来设置UITabBarController:

AppDelegate.m文件:

#import "PickerDelegate.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ABPeoplePickerNavigationController *contacts = [[ABPeoplePickerNavigationController alloc] init];
    PickerDelegate *pickerDel = [[PickerDelegate alloc] init];
    contacts.delegate = pickerDel;

    NSArray *aViewControllers = [NSArray arrayWithObjects:xvc, contacts, yvc, zvc, nil];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:aViewControllers];
    [xvc release];
    [contacts release];
    [yvc release];
    [zvc release];

    [window setRootViewController:tabBarController];
    [tabBarController release];
    [self.window makeKeyAndVisible];

    return YES;
}

PickerDelegate.h文件

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface PickerDelegate : NSObject <UINavigationControllerDelegate, ABPeoplePickerNavigationControllerDelegate>

@property (nonatomic, assign) PickerDelegate *delegate;

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;

@end

最后,PickerDelegate.m文件:

#import "PickerDelegate.h"

@implementation PickerDelegate

@synthesize delegate = _delegate;

#pragma mark ABPeoplePickerNavigationControllerDelegate methods

// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
     NSLog(@"shouldContinueAfterSelectingPerson");

     //...
     return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSLog(@"shouldContinueAfterSelectingPerson");

    //...
    return NO;
}

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    NSLog(@"peoplePickerNavigationControllerDidCancel");

     //...
}
@end

但它不起作用,我的方法没有调用。缺少什么?

1 个答案:

答案 0 :(得分:7)

ABPeoplePickerNavigationController的委托属性为 peoplePickerDelegate ,而不是委托

这样做是为了让它发挥作用..

contacts.peoplePickerDelegate = pickerDel;