1

我有很多.xib文件,我嘗試以編程方式重新創建它們。navigationBar顏色和標題

唯一的問題是,我無法再自定義我的導航欄。

De版本函數被另一個視圖與多個bar按鈕項調用,所以代碼對其他函數通用。

- (void)versions 
{ 
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document]; 
    [verController setDelegate:self]; 

    [self loadPopupView:verController]; 

    [verController release]; 
} 

- (void)loadPopupView:(UIViewController *)viewController 
{ 
    if (popOverController != nil && [popOverController isPopoverVisible]) 
    { 
     [popOverController dismissPopoverAnimated:YES]; 
    } 

    if(![popOverController isPopoverVisible] || ![popOverController.contentViewController isKindOfClass:[viewController class]]) 
    { 
     popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController]; 
     popOverController.popoverContentSize = CGSizeMake(320, 500); 

     UIBarButtonItem *buttonLocation; 

     if([viewController isKindOfClass:[CommentaryViewController class]]) 
      buttonLocation = commentaryButton; 
     else if([viewController isKindOfClass:[PropertiesViewController class]]) 
      buttonLocation = propertiesButton; 
     else 
      buttonLocation = versionsButton; 

     [popOverController presentPopoverFromBarButtonItem:buttonLocation permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 
} 

VersionsViewController.m

- (id)initWithDocument:(Document *)doc 
{ 
    self = [super init]; 

    if(self) 
    { 
     self.document = doc; 
     self.title = @"other title"; //does not work either 

     //I just tried everything I could think of :P 
     self.navigationController.navigationBar.tintColor = [UIColor orangeColor]; 
     self.navigationItem.titleView.backgroundColor = [UIColor redColor]; 
     self.navigationController.navigationItem.titleView.backgroundColor = [UIColor greenColor]; 
     self.navigationController.tabBarController.tabBar.backgroundColor = [UIColor blueColor]; 
     self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor]; 
    } 

    return self; 
} 

有人可以看到我做錯了什麼?

編輯:

從self.title的NSLog和self.navigationController.title都是「空」

當我創建一個navigationController添加視圖,並添加navigationController到彈出我得到2個酒吧和那麼我可以設置navigationController的標題,但仍然不是顏色。

回答

2

只是好奇:如果你有工作nib文件,爲什麼你以編程方式重新創建它們?您還沒有分配nvaigationController。您需要手動構建其中一個,然後在其中安裝VersionViewController(完成後將設置navigationController)。讓所有這些自動工作是我們使用nib文件的原因之一。

編輯任何時候「沒有任何反應」,檢查您收到的消息不是nil。我敢肯定navigationController仍然是nil當你到達它。您還應該關注您關注的對象並查看其地址。您可能無意中創建了多個視圖或多個導航控制器。當您嘗試手動構建這些東西時,這很常見。在大多數情況下,筆尖是正確的解決方案。

+0

以編程方式創建它們的原因是性能。 .xib文件只是很大的XML文件,我只需要一個導航欄和一個表格視圖,沒什麼複雜的。奇怪的是,我看到的標題女巫從未設置酒吧。當我創建一個navigationController添加視圖,並將navigationController添加到彈出我得到2酒吧,然後我可以設置navigationController的標題,但仍然不是顏色。 – Justin

+1

您不會通過刪除nib文件來提高性能。 Nib文件被編譯。 XIB文件是它們的XML源代碼。有一個小的磁盤訪問成本可以提高內存使用率。如果出現問題,可以通過訪問視圖控制器的view屬性來預加載筆尖。解凍筆尖通常比生成物體便宜,因爲所有的值都是凍乾的。但是在這兩種情況下的差別都很小,而不是性能優化的地方。 –

+0

非常感謝您的好解釋。我想我只是重新創建nib文件,如果沒有它們,性能不是那麼好。無論如何,這需要很多時間。再次感謝! – Justin

0
- (void)versions 
{ 
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document]; 
    [verController setDelegate:self]; 
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:verController]; 
    [self loadPopupView:navC]; 
    [verController release]; 
    [navC release]; 
}