tags:

views:

2137

answers:

1

I'm an experienced software developer (mostly C and C++ in embedded systems, Linux, and Qt for user interfaces). I've recently made the jump to iPhone development and I've run into a confusing issue with UIViewController and presentModalViewController. I'm trying to present a modal ABPeoplePickerNavigationController dialog but it's not appearing.

My main application is controlled by an object called Controller, a subclass of NSObject. I recently added a subclass of UIViewController called AddressBookController (both Controller and AddressBookController are declared in my NIB).

I have this function in Controller which uses an IBOutlet to call a function in AddressBookController (Just a side note, I'm aware that passing 'sender' through is probably not correct--what's the correct way to call a function that's looking for (id)sender as a param?):

- (IBAction)addressPressed:(id)sender
{
    [addressViewController showContactPicker:sender];
}

Here's my interface declaration for AddressBookViewController:

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

@interface AddressBookViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
{
}

@end

The showContactPicker function in AddressBookController looks like this:

#import "AddressBookViewController.h"

@implementation AddressBookViewController

- (void) showContactPicker:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

...

I've verified using the debugger that the [self presentModalViewController:picker animated:YES] is being called, but nothing appears on the screen and code execution just proceeds to [picker release]. Does anyone have any insight as to why this call isn't doing anything? I'm not seeing any error messages, warnings, etc. as I run through this code.

Thanks in advance!

+1  A: 

You don't specify what nib/xib to use in your init:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

Change it to:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] initWithNibName@"ABPeoplePickerNavigationController" bundle:nil];

making sure you use whatever your nib file is named, minus the .xib extension.

Matt Long
Thanks, I gave that a shot but it didn't change anything; it still doesn't display the address book picker.I only have one .xib file in my project, MainWindow.xib, so I changed the line to this: ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] initWithNibName:@"MainWindow" bundle:nil];What does that do? I read the documentation on initWithNibName but all it says is "Returns a newly initialized view controller with the nib file in the specified bundle."Thanks again for your help.Jason
Maha
Sorry to mislead you here. I haven't used the Address Book API and didn't recognize that at first. You shouldn't need to specify the xib when you initialize the ABPeoplePickerNavigationController. I put together a sample project and implemented the ABPeoplePickerNavigationController and was able to get it to work just fine (http://www.matthew-long.com/download/AddressBook.zip ). Did you implement all of the methods of the ABPeoplePickerNavigationControllerDelegate formal protocol?
Matt Long
Thanks for the example. I'll compare it to my code and try to nail down what's wrong with it.
Maha
The example helped a lot--the problem was in how my application was structured. My AddressBookViewController wasn't being added as a subview to my main window; it's working now.Thanks again!
Maha