簡體   English   中英

UINavigationController標題不顯示?

[英]UINavigationController title is not displayed?

我有正常的UIViewController,其中我添加了UINavigationControllerDelegate,我在willappear中添加如下?但它不起作用?

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

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

即在添加視圖控制器中

- (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];

每個視圖控制器都有一個導航項。 您正在更改導航控制器的導航項...但除非導航控制器位於另一個導航控制器內,否則永遠不會看到它! 代替

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

你要

self.navigationItem.title = @"hai";

或者,如果導航項目的標題為零,導航控制器將使用您的視圖控制器的標題:

self.title = @"hai";

您應該只設置視圖標題,導航欄和標簽欄都使用它,除非您因某些原因要為每個標題指定不同的標題。

在我的情況下,當我在init方法中設置時,標題在iOS6上不可見:

- (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

您可能需要在控制器的“屬性”選項卡中將導航欄設置為“帶有提示的黑色導航欄”。 這對我有用。

設置標題時,你確定self.navigationController.navigationItem不是nil嗎?

如果沒有將控制器推入堆棧,即。 你可以在堆棧的頂部顯示viewcontroller(有時稱為rootViewController)

[rootViewController setTitle:@"Title"];

請注意,您必須使用setTitle - rootViewController.title = @“Title”通常不起作用。

實際上,你可以使用任何viewcontroller,即使它還沒有viewDidLoad。 如果它是以編程方式創建的,則只需創建它即可

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

或者您可以將其聲明為app委托中的對象,將其設置為IBOutlet,創建UIViewController並將其類設置為myViewController,並將其連接到IBOutlet,因此它不是nil。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM