快速更改后退按钮的文本

时间:2015-11-12 14:40:34

标签: ios iphone swift back navigationbar

我在导航栏中更改后退按钮的文本时出现了一个小问题。我的应用程序看起来像这样:

- > Navigation Controller1 - > ViewController1 - > ViewController2 - 显示详细信息> TabBarController - > NavigationController2 - > ViewController3 - > ViewController4

我通过添加

更改了ViewController2的后退按钮的文字
self.navigationItem.backBarButtonItem?.title = "Test"

viewDidLoad()进入ViewController1方法。但我无法从ViewController4

更改后退按钮的文字

我做了一些研究并找到了一些解决方案,但它们并没有为我工作。

我试过了:

App Delegate - 我遇到了崩溃:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Please file a radar on UIKit if you see this assertion.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Code       
 UIBarButtonItem.appearance().title = "Test"
//Code 
return true 
}

在推送ViewController3中 - 什么都没有改变

    self.navigationItem.backBarButtonItem?.title = "Test"

NavigationController2中 - 什么都没有改变

    self.navigationItem.backBarButtonItem?.title = "Test"

NavigationController2中 - 什么都没有改变

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

在我的故事板中更改NavigationItem中的文字 - 没有任何反应

最好的解决方案是在App Delegate中设置它。但我不能因为它崩溃......

你有什么想法我做错了吗?

Crashlog:

2015-11-12 15:51:56.444 Name[2988:417662] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x22718f8f 0x30dc9c8b 0x22632c2b 0x262e55e7 0x260358b1 0x25f03439 0x262e2c83 0x25e40baf 0x25e406bb 0x2603f153 0x2603f0cb 0x25d9fd27 0x2605b987 0x2603efb7 0x25d9779d 0x25d96dc7 0x25d96ce1 0x25da154b 0x25da0f6b 0x25e2cf1f 0x25e2c917 0x25e2c6dd 0x25e2c19f 0x25e44a75 0x25e446b5 0x25e44649 0x25d97e43 0x257abd29 0x257a755d 0x257a73e5 0x257a6d81 0x257a6b6f 0x257a0849 0x226dedcd 0x226dc48b 0x226dc893 0x22628f31 0x22628d43 0x2a103201 0x25dfa879 0x10b778 0x3137baaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

4 个答案:

答案 0 :(得分:4)

当目标控制器的后栏按钮的标题自动显示源控制器的导航项的标题时,一种方法是在源视图控制器中设置导航项的标题

  • viewWillAppear()viewDidAppear()到正常标题。
  • prepareForSegue()中标题后面栏按钮应该显示。

答案 1 :(得分:1)

这就是我这样做的方式

为文件顶部的栏按钮创建一个新变量:

SELECT RecordID, RoomID FROM Jafa WHERE RoomID = $value //MultiRoomSelect[]

然后设置按钮的属性

var leftBarButton: UIBarButtonItem!

最后在导航控制器中设置新的条形按钮

self.leftBarButton = UIBarButtonItem(title: "Your title", style: .Plain, target: self, action: "leftButtonAction")

希望它有所帮助!

答案 2 :(得分:0)

你已经概述了segues的方式,看起来ViewController3根本没有后退按钮。 显示详细信息 segue会替换UISplitViewController中的子视图控制器,否则会以模态方式显示子视图。无处可去。

在ViewController3上使用viewDidLoad内部进行此操作(使用pre-Swift 2语法):

if let _ = self.navigationItem.backBarButtonItem {
    print("not nil")
} else {
    print("nil")
}

当我在示例项目上尝试时,它没有。我认为这是核心问题。

这一点hackery有效:

if let navButtons = self.navigationController?.navigationBar.items {
    if navButtons.count > 0 {
        navButtons[0].title = "Test"
    }
}

然而,这非常糟糕。操纵呈现ViewController的标题(按@ vadian'建议)显然更清晰。

答案 3 :(得分:0)

此解决方案对我有用:(在推送的ViewController的viewDidLoadviewWillAppear中使用此

let backButton = UIBarButtonItem()
    backButton.title = "Your text"
    backButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .normal)
    navigationController?.viewControllers[(navigationController?.viewControllers.count)! - 2].navigationItem.backBarButtonItem = backButton
相关问题