Jason
Jason

Reputation: 43

ABPeoplePickerNavigationController and UITableViewController in a UITabBarController

Please see below for my own answer, I figured it out after hours and hours. Hopefully I save someone some time.

I currently have a button on my UI that will modally present an ABPeoplePickerNavigationController, which works perfectly fine. I want to extend this to implement favorites and recents, so that when the user hits the button I present a UITabBarController that has the ABPeoplePickerNavigationController on one tab, and then two other tabs with the favorites and recents (which I'm guessing will be UITableViewControllers. I basically want the tab bar controller to behave like a modal version of the built in phone app for the contacts, favorites and recents tabs (but not for just phone numbers).

I have searched all over trying to get a solution on how to do this (I'm new to TabBarControllers), and have thus far tried to do it programmatically with a little bit of success, and in Interface Builder with absolutely no success, all I ever see is a white screen presented.

Is there some library that already does something close to this that I just can't seem to find for contacts and favorites?

Here is breakdown on what I've tried in both methods, and how it worked:

Programmatically: I basically create the ABPeoplePickerNavigationController like I was going to present it modally alone, but instead add it to a UITabBarController instance using the setViewControllers method. When I present this, the tab says "Groups" on it, and I can't figure out how to change the icon to the system icon for contacts, or change the behavior of pressing the tab bar button to not back out into the groups (the built in phone app doesn't back up to that level when you press the tab bar button). Like I mentioned above, I basically want it to behave similarly to the built in phone app for contacts, favorites and recents.

IB: I tried a ton of things and always only get a white screen when trying to present it modally.

Upvotes: 0

Views: 1297

Answers (1)

Jason
Jason

Reputation: 43

I got two tabs, one with the ABPEoplePickerNavigationController and one with a table view working last night. I hope this helps someone. Note that you still have to add the protocols to the current view controller for the picker and the two for the tableview, and then add the delegate functions for each into your code. If you don't know which protocols to adopt or functions to write, check out the apple dev docs for each.

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
UITabBarItem *peoplePickerTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
picker.tabBarItem = peoplePickerTabBarItem;
UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tvc.tableView.delegate = self;
tvc.tableView.dataSource = self;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tvc];
UIBarButtonItem *uibbiCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTable)];
tvc.navigationItem.rightBarButtonItem = uibbiCancel;
tvc.title = @"Recents";
UITabBarItem *nvcTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
nvc.tabBarItem = nvcTabBarItem;
tbc = [[UITabBarController alloc] init];
NSArray *sections = [[NSArray alloc] initWithObjects:picker, nvc, nil];
[tbc setViewControllers:sections];
[self presentModalViewController:tbc animated:YES];
[nvcTabBarItem release];
[uibbiCancel release];
[tvc release];
[peoplePickerTabBarItem release];
[picker release];
[nvc release];
[sections release];
[tbc release];

Upvotes: 2

Related Questions