2016-04-26 170 views
1

我有一個有一些後退標題「XYZ」的VC,在viewDidLoad中,我將它更改爲「ABC」。然後我想用相同的方法再次更改它,但它不起作用。嘗試了很多解決方案,但沒有成功。快速更改按鈕標題後的後退按鈕標題

我找到了這段代碼。在viewdidload中喚醒......但在此之後它不。

let backButton = UIBarButtonItem(
     title: " ", 
     style: UIBarButtonItemStyle.Plain, 
     target: nil, 
     action: nil 
    ) 
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton 

enter image description here

回答

1

UIViewController類執行此操作。這會留下後退箭頭,但會移除旁邊的文字。

private var _title: String? 
var backBarButtonHidden: Bool { 
    didSet { 
     if let index = navigationController?.viewControllers.indexOf(self) { 
     if index > 0 { 
      if let button = navigationController?.navigationBar.items?[index - 1] { 
       if backBarButtonHidden { 
        _title = button.title 
        button.title = "" 
       } else { button.title = _title } 
      } 
     } 
    } 
} 
+0

那麼這工作時,我不添加我的一段代碼在viewdidload。但是,我無法再將標題添加到按鈕。 : -/ –

+0

是的,你可以,你將不得不存儲在一個私人變量的標題,並以完全相同的方式訪問標題: –

+0

啊哈....哇現在這是我想要的:-)非常感謝你。你讓我很開心:-) –