2009-06-22 102 views
6

我想用'ABPeoplePickerNavigationController'来启动一个模态视图控制器,而不需要创建一个包含视图控制器的导航控制器。启动一个模态UINavigationController

做类似的事情会产生一个空白的屏幕,导航栏没有标题,即使我在调用'init'时调用initWithNibName,也没有为视图加载关联的nib文件。

我的控制器看起来像:

@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 

当使用AB控制器,你要做的就是:

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

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

的ABPeoplePickerNavigationController声明为:

@interface ABPeoplePickerNavigationController : UINavigationController 

的另一种方式创建一个模式视图,如苹果的'视图控制器编程指南“中所建议的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]; 

我可以创造这种方式细(只要我改变MyViewController为继承的UIViewController而不是UINavigationController的)。我还应该如何对MyViewController启动与ABPeoplePickerNavigationController相同的方式?

回答

4

我想推出一个模式视图控制器“的ABPeoplePickerNavigationController”一个确实的方式,那就是不必创建一个包含视图控制器

导航控制器,但是,这正是的ABPeoplePickerNavigationController是在做。这并不神奇,它是一个UINavigationController,它在内部实例化一个UIViewController(一个UITableView与你的地址簿联系人一起填充),并将UIViewController设置为其根视图。

你确实可以创建你自己的类似的UINavigationcontroller子类。但是,在它的初始化程序中,您将需要创建一个视图控制器来加载其根视图,就像ABPeoplePickerNavigationController一样。

然后,你可以做你正在尝试这样的东西:

[self presentModalViewController:myCutsomNavigationController animated:YES]; 

在您发布的代码:

@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 

我怀疑你有NIB的问题。没有连接的“rootViewController”插座。这就是为什么你有一个空白的屏幕。

你应该在内部使用的initalizer是这样的:

self = [super initWithRootViewController:myCustomRootViewController]; 
相关问题