2010-01-11 145 views
20

我有正常的UIViewController,其中我添加了UINavigationControllerDelegate,我添加如下willappear?但它不起作用?UINavigationController標題不顯示?

[self.navigationController setNavigationBarHidden:NO animated:YES]; 
    self.navigationController.navigationItem.title = @"hai"; 

回答

43

您應該在添加到導航控制器的視圖控制器中設置標題。

即在添加視圖控制器

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.title = @"My Title"; 
} 

或訪問您的viewcontrollers的陣列直接

 AddFriendViewController *addFriendsView = [[AddFriendViewController alloc] initWithNibName:@"AddFriendViewController" bundle:nil]; 
     [self.navigationController pushViewController:addFriendsView animated:YES]; 
     [[self.navigationController.viewControllers objectAtIndex:0] setTitle:@"myTitle"]; 
     [addFriendsView release]; 
0

你肯定self.navigationController.navigationItem不是nil當設置標題?

14

每個視圖控制器都有一個導航項目。您正在更改導航控制器的導航項...但除非導航控制器位於另一個導航控制器內,否則將無法看到!取而代之的

self.navigationController.navigationItem.title = @"hai"; 

你想

self.navigationItem.title = @"hai"; 

,或者,如果您的導航項目的標題是零,導航控制器會根據您的視圖控制器的標題:

self.title = @"hai"; 

您應該簡單地設置查看標題,導航欄和標籤欄都使用該標題,除非您想爲某些原因爲每個標題指定不同的標題。

0

如果沒有viewcontroller被推入堆棧,即。你在顯示棧(有時稱爲RootViewController的)的頂部視圖 - 控制你可以,你必須使用的setTitle

[rootViewController setTitle:@"Title"]; 

注意做到這一點 - rootViewController.title = @「標題」,通常會無法正常工作。

其實,你可以用任何viewcontroller來做,即使它不是viewDidLoad的。如果它的編程方式創建,只是

myViewController *mvc=[[myViewController alloc] initWithNibName:@"nameofnibwithout.xib" bundle:nil]; 

創建,或者你可以聲明其作爲應用程序的委託對象,使其成爲一個IBOutlet,創建一個UIViewController並設置其類myViewController,並將其連接到IBOutlet中所以它不是零。

0

您可能需要在控制器上的「屬性」選項卡中將導航欄設置爲「帶提示的黑色導航欄」。這對我來說很有用。

1

在我的情況下,標題是不可見的iOS6,當我在init方法對其進行設置:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
     self.title = LS(@"Favorites"); 
     self.tabBarItem.title = LS(@"Favorites"); 
     self.tabBarItem.image = [UIImage imageNamed:@"star"]; 
    } 

    return self; 
} 

因此,它應該被從那裏ViewDidLoad