【问题标题】:Launching a modal UINavigationController启动模态 UINavigationController
【发布时间】:2010-05-16 18:00:07
【问题描述】:

我想像使用“ABPeoplePickerNavigationController”一样启动一个模态视图控制器,而无需创建包含视图控制器的导航控制器。

执行类似操作会产生一个空白屏幕,导航栏没有标题,并且即使我在调用“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

Apple 的“视图控制器编程指南”中建议的创建模态视图的另一种方法 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 相同的方式启动?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    我想像使用“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”插座。这就是为什么你有一个空白屏幕。

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

    self = [super initWithRootViewController:myCustomRootViewController];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多