Tuesday, July 13

如何在Tab Bar Controller里面使用ABPersonViewController

如果你想将ABPersonViewController嵌入一个Tab Bar Controller里作为一个Tab,就像下图,该如何作呢?



Step By Step

1:制作xib的时候,不要在相应的tab里加入view controll,如下图:


2,创建自己的NavigationController,继承自UINavigationController,加入ABPersonViewControllerDelegate的protocol


@interface ProfileNavigationController : UINavigationController {


}


3,在自己的NavigationController里面相应 -(void)awakeFromNib,这个方法在xib被调入内存的时候,会被执行,在其中调用 initWithRootViewController,就可以将需要的ViewController显示出来。




-(void)awakeFromNib


-(void)awakeFromNib
{
NSLog(@"bundleLoader waked");
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person named "Appleseed" in the address book
CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("Appleseed"));
// Display "Appleseed" information if found in the address book
if ((people != nil) && (CFArrayGetCount(people) > 0))
{
ABRecordRef person = CFArrayGetValueAtIndex(people, 0);
ABPersonViewController personPicker = [[[ProfileViewController alloc] init] autorelease];
personPicker.personViewDelegate = self;
personPicker.displayedPerson = person;
// Allow users to edit the person’s information
personPicker.allowsEditing = YES;

[self initWithRootViewController:personPicker];
}
else
{
// Show an alert if "Appleseed" is not in Contacts
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not find Appleseed in the Contacts application"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
CFRelease(addressBook);
CFRelease(people);
}

No comments: