Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C address book

I have two Objective-C classes that inherit from the UIViewController and am trying a different approach at learning how to interact with the iPhone's address book. The example Apple provides assumes that everything is in one class, but this isn't the way I need it done. My objective would be to have the address book view close after a person is selected. Please have a look and let me know how i can accomplish this without having CallerClass implement ABPeoplePickerNavigationControllerDelegate. Thanks!

-- edit --

What it seems to be boiling down to is the [self dismissModalViewControllerAnimated:YES]; does not have any effect in CalleeClass.m. I still can't seem to get a reaction to close the address book from this command.

CallerClass.m

#import "CallerClass.h"

@implementation CallerClass
- (IBAction)openAddressBook {
    CalleeClass *cc = [[CalleeClass alloc] init];
    [self presentModalViewController:[cc doIt] animated:YES];
}

CalleeClass.h

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

@interface CalleeClass : UIViewController <ABPeoplePickerNavigationControllerDelegate> {
    NSString *name;
}

-(ABPeoplePickerNavigationController *)doIt;

@property (nontoxic, retain) NSString *name;

@end

CalleeClass.m

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "CalleeClass.h"

@implementation CalleeClass
@synthesize name;

… (default ABPeoplePickerNaviationControllerDelegate implementation outside of what's listed)

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {}
    return self;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    self.name = (NSString *)ABRecordCopyValue(person,kABPersonAddressProperty);

    [self dismissModalViewControllerAnimated:YES];
    return NO;  
}

-(ABPeoplePickerNavigationController *)doIt {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    return picker;
}

@end
like image 380
Mat Kelly Avatar asked Jan 23 '26 17:01

Mat Kelly


1 Answers

If the problem is, as you say, that [self dismissModalViewControllerAnimated:YES] has no effect if called from CalleeClass, this is because dismissModalViewControllerAnimated: must be called on the presenting view controller (i.e., the one on which you called presentModalViewController:Animated:. Since you don't have a reference to your CallerClass instance in CalleeClass, this doesn't work.

Fortunately, as the documentation for dismissModalViewControllerAnimated: notes:

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

So this should work:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    self.name = (NSString *)ABRecordCopyValue(person,kABPersonAddressProperty);
    [peoplePicker dismissModalViewControllerAnimated:YES];
    return NO;  
}
like image 100
Ole Begemann Avatar answered Jan 26 '26 07:01

Ole Begemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!