启动模态UINavigationController

Ale*_*ove 6 iphone

我想像'ABPeoplePickerNavigationController'一样启动模态视图控制器,而不必创建包含视图控制器的导航控制器.

做类似的操作产生一个空白屏幕,没有导航栏的标题,并且没有为视图加载相关的nib文件,即使我在调用'init'时调用initWithNibName.

我的控制器看起来像:

@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
Run Code Online (Sandbox Code Playgroud)

使用AB控制器时,您所做的只是:

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

[self presentModalViewController:picker animated:YES];
[picker release];
Run Code Online (Sandbox Code Playgroud)

ABPeoplePickerNavigationController声明为:

@interface ABPeoplePickerNavigationController : UINavigationController
Run Code Online (Sandbox Code Playgroud)

另一种创建模态视图的方法,如Apple的"适用于iPhone OS的View Controller编程指南"中所述:

// 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];
Run Code Online (Sandbox Code Playgroud)

我可以这样创建它(只要我改变MyViewController继承UIViewController而不是UINavigationController).我还应该对MyViewController做什么来启动与ABPeoplePickerNavigationController相同的方式?

Cor*_*oyd 4

我想以“ABPeoplePickerNavigationController”的方式启动模式视图控制器,而无需创建包含视图控制器的导航控制器

但这正是 ABPeoplePickerNavigationController 正在做的事情。这并不神奇,它是一个 UINavigationController,它在内部实例化一个 UIViewController(一个填充有地址簿联系人的 UITableView)并将 UIViewController 设置为其根视图。

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

然后你可以像这样做你正在尝试的事情:

[self presentModalViewController:myCutsomNavigationController animated:YES];
Run Code Online (Sandbox Code Playgroud)

在您发布的代码中:

@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
Run Code Online (Sandbox Code Playgroud)

我怀疑你有 NIB 问题。没有可连接的“rootViewController”插座。这就是您出现空白屏幕的原因。

您应该在内部使用的初始化器是这样的:

self = [super initWithRootViewController:myCustomRootViewController];
Run Code Online (Sandbox Code Playgroud)