Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching a modal UINavigationController

Tags:

iphone

I'd like to launch a modal view controller the way one does with 'ABPeoplePickerNavigationController' and that is without having to creating a navigation controller containing the view controller.

Doing something similar yields a blank screen with no title for the navigation bar and there's no associated nib file loaded for the view even though I am invoking the initWithNibName when the 'init' is called.

My controller looks like:

@interface MyViewController : UINavigationController

@implementation MyViewController
- (id)init {
    NSLog(@"MyViewController init invoked");
    if (self = [super initWithNibName:@"DetailView" bundle:nil]) {
        self.title = @"All Things";
    }
    return self;
}
- (void)viewDidLoad {   
    [super viewDidLoad];

    self.title = @"All Things - 2";
}

@end

When using the AB controller, all you do is:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;

[self presentModalViewController:picker animated:YES];
[picker release];

ABPeoplePickerNavigationController is declared as:

@interface ABPeoplePickerNavigationController : UINavigationController

The other way to create a modal view as suggested in Apple's 'View Controller Programming Guide for iPhone OS':

// Create a regular view controller.
MyViewController *modalViewController = [[[MyViewController alloc] initWithNibName:nil bundle:nil] autorelease];

// Create a navigation controller containing the view controller.
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:modalViewController];

// Present the navigation controller as a modal view controller on top of an existing navigation controller
[self presentModalViewController:secondNavigationController animated:YES];

I can create it this way fine (as long as I change the MyViewController to inherit from UIViewController instead of UINavigationController). What else should I be doing to MyViewController to launch the same way as ABPeoplePickerNavigationController?

like image 380
Alexi Groove Avatar asked Jun 22 '09 22:06

Alexi Groove


1 Answers

I'd like to launch a modal view controller the way one does with 'ABPeoplePickerNavigationController' and that is without having to creating a navigation controller containing the view controller

But this is exactly what ABPeoplePickerNavigationController is doing. It isn't magic, it is a UINavigationController that instantiates a UIViewController internally (a UITableView that is populated with your address book contacts) and sets the UIViewController as its root view.

You can indeed create your own similar UINavigationcontroller subclass. However, within it's initializer, you will need to create a view controller to load as its root view just like ABPeoplePickerNavigationController does.

Then you can do what you are trying like this:

[self presentModalViewController:myCutsomNavigationController animated:YES];

In the code you posted:

@interface MyViewController : UINavigationController

@implementation MyViewController
- (id)init {
    NSLog(@"MyViewController init invoked");
    if (self = [super initWithNibName:@"DetailView" bundle:nil]) {
        self.title = @"All Things";
    }
    return self;
}
- (void)viewDidLoad {   
    [super viewDidLoad];

    self.title = @"All Things - 2";
}

@end

I suspect you are having NIB issues. there isn't a "rootViewController" outlet to connect. This is why you have a blank screen.

The initalizer you should be using internally is this:

self = [super initWithRootViewController:myCustomRootViewController];
like image 127
Corey Floyd Avatar answered Sep 30 '22 00:09

Corey Floyd