iphone - 启动模态 UINavigationController

标签 iphone

我想像使用“ABPeoplePickerNavigationController”那样启动模态视图 Controller ,而无需创建包含 View Controller 的导航 Controller 。

执行类似的操作会产生一个空白屏幕,导航栏没有标题,并且没有为 View 加载关联的 nib 文件,即使我在调用“init”时调用 initWithNibName 也是如此。

我的 Controller 看起来像:

@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 Controller 时,您要做的就是:

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

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

ABPeoplePickerNavigationController 声明为:

@interface ABPeoplePickerNavigationController : UINavigationController

按照 Apple 的“ View Controller 编程指南”中的建议创建模态视图的另一种方法 iPhone 操作系统:

// 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 相同的方式启动?

最佳答案

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

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

您确实可以创建自己的类似 UINavigationcontroller 子类。但是,在其初始化程序中,您需要创建一个 View Controller 来加载为其 Root View ,就像 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” socket 。这就是您出现空白屏幕的原因。

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

self = [super initWithRootViewController:myCustomRootViewController];

关于iphone - 启动模态 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1029779/

相关文章:

ios - 如何在 IOS 中添加动画启动画面?

iphone - 如何在我的 iPhone 应用程序中创建 UIView "scrollable"?

iphone - 将 UITextField 标签转换为字符串

ios - TTAtributedLabel 设置多色使用 UIColor,它在 header 中用 MACRO 定义?

iPhone 5 无法正常显示应用程序

iphone - 通过monotouch将iPhone应用程序部署到单个电话的最低成本是多少?

iphone - 识别绘制图像的最佳方法

iphone - 如何验证字符串是否为字母数字

iphone - 如何在iPhone导航栏中添加搜索栏?

iPhone 核心数据 - 导入附加内容