2009-06-22 103 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]; 
相關問題