如何通过调用方法而不是按钮单击来获取打开的联系人

时间:2011-06-06 06:59:21

标签: iphone

根据我的要求,

我需要从联系人处获取电子邮件ID。

我需要在一个方法的单独的类中编写一个代码。为了调用这个将类集成到我的项目中只需调用该方法。

这就是我需要的。

为此我在ownServices中的代码是这样的。

-(NSString *)getSelectedNumberFromContatcs {
    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];

    peoplePickerController.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePickerController animated:NO];
    [peoplePickerController release];

    return aNSString;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    //  NSString *name = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);

    return YES;
}

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

    if (property == kABPersonPhoneProperty) {
        ABMultiValueRef emails = ABRecordCopyValue(person, property);
        CFStringRef phonenumberselected = ABMultiValueCopyValueAtIndex(emails, identifier);
        //  CFStringRef emailLabelSelected = ABMultiValueCopyLabelAtIndex(emails, identifier);
        //  CFStringRef emailLabelSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, identifier));

        aNSString = (NSString *)phonenumberselected;

        // Return to the main view controller.
        [ self dismissModalViewControllerAnimated:YES ];
        return NO;
    }   
    return YES ;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [ self dismissModalViewControllerAnimated:YES ];

}

我在myclassviewcontroller中这样调用它。

- (void)viewDidLoad {
    [super viewDidLoad];
    ownServices *obj = [[ownServices alloc]init];
    [obj getSelectedNumberFromContatcs];

    }

但是没有打开contatcs viewcontraoller。

但我在视图控制器中尝试使用像这样的按钮操作中的相同代码

-(IBAction)openContacts {
    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];

    peoplePickerController.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePickerController animated:NO];
    [peoplePickerController release];

} 

然后联系人viewconrtroller打开。

我没有通过在方法中调用它来打开控制器的原因。

是否可以这样做。

任何人都可以帮助我。

提前感谢你。

2 个答案:

答案 0 :(得分:0)

而不是peoplePickerController.peoplePickerDelegate = self是否可以使用主视图控制器的引用?

答案 1 :(得分:0)

因为你使用的类是继承自NSObject而不是UIViewController所以[self presentModalViewController:peoplePickerController animated:NO];不管用。也  你以错误的方式写了这个方法

-(NSString *)getSelectedNumberFromContatcs {
    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];

    peoplePickerController.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePickerController animated:NO];
    [peoplePickerController release];

    return aNSString;
}

让我知道你究竟想做什么?