iOS中

2017-04-04 46 views
0
在viewDidAppear更改NavigationItem標題

我已經設置在Interface Builder中NavigationItem.title爲了可讀性,代碼我需要做的iOS中

self.navigationItem.title = SomeThingThatComesFromDB 

我試圖做到這一點在viewDidLoadviewDidAppear,但在第一個應用程序啓動標題仍然是我在界面構建器中設置的。

我該如何解決這個問題?

這是應用程序啓動我的根視圖控制器,這裏是我使用的代碼:

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    for (Branch *b in [UserManager sharedInstance].branches) { 
     if ([b.branchId isEqualToString: [UserManager sharedInstance].vendorId]) { 
      self.title = b.branchName; 
     } 
    } 
} 
+0

嘗試self.title = SomeThingThatComesFromDB –

+0

@Anbu。 Karthik我已經試過,也在視圖中DidAppear – shaibow

+0

顯示你嘗試過的代碼,首先你的VC被嵌入到導航控制器中 –

回答

0

你進來的地方設置/更新的導航項目的標題下面的對比。

if ([b.branchId isEqualToString: [UserManager sharedInstance].vendorId]) { 
    self.title = b.branchName; 
} 

問題似乎是在[的UserManager sharedInstance] .vendorId]變量沒有設置(還),當你調用該函數更新導航項目的冠軍。正如你能夠正確設置,在視圖消失並再次出現之後。


下面的代碼再一次說明了一個如何設置一個導航項目的冠軍,這也是你做的事:

如果要更新一般標題爲所有相關的導航元素(的UITabBarController標籤,UINavigationController的後退按鈕等),只需使用:

self.navigationItem.title = SomeThingThatComesFromDB 

https://developer.apple.com/reference/uikit/uiviewcontroller/1621364-title

如果你有一個專用的導航項目您的UINavigationController的酒吧,你可以訪問它下面的方式(請確保,只有一個訪問)

self.navigationItem.rightBarButtonItem.title = SomeThingThatComesFromDB 
self.navigationItem.leftBarButtonItem.title = SomeThingThatComesFromDB 
+0

這裏的問題是任何人在這個問題上的所有方法工作!但不是在第一次午餐!我必須去另一個頁面,然後回來使它工作... – shaibow

+0

你什麼時候設置「[UserManager sharedInstance] .vendorId」?我認爲這個值可能是空的,直到數據從某個地方加載,然後也不能匹配 – Lepidopteron

+1

這是真的!我突出了這一點,並看到了問題! – shaibow

0

嘗試下面的代碼:

CGRect rect = self.navigationController.navigationBar.frame; 

UIView *myView = [[UIView alloc] init]; 
UILabel *title = [[UILabel alloc] init]; 
[title setFont:[UIFont systemFontOfSize:18.0]]; 
title.text = *SomeThingThatComesFromDB*; 
title.textColor = [UIColor whiteColor]; 

CGSize size = [title.text sizeWithAttributes: 
       @{NSFontAttributeName: 
        title.font}]; 

title.frame = CGRectMake(20, 0, size.width, rect.size.height);//40 
myView.frame = CGRectMake(0, 0, 40+size.width, rect.size.height); 

[myView addSubview:title]; 
self.navigationItem.titleView = myView;