简体   繁体   中英

Swift change text of back button

I have a small problem with changing the text of the back button in my navigation bar. My App looks like this:

-> Navigation Controller1 -> ViewController1 -> ViewController2 -Show Detail> TabBarController -> NavigationController2 -> ViewController3 -> ViewController4

I changed the text of the back button for my ViewController2 by adding

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

into the viewDidLoad() method from ViewController1 . But I can't change the text of the back button from ViewController4

I made some research and found a few solutions but they didn't work for me.

I tried:

In the App Delegate - I am getting a crash: 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 
}

In the pushed ViewController3 - Nothing is changing

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

In NavigationController2 - Nothing is changing

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

In NavigationController2 - Nothing is changing

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

In my Storyboard changing the text in the NavigationItem - Nothing happens

The best solution would be to set it in the App Delegate . But I can't because it is crashing...

Do you have any ideas what I am doing wrong?

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

As the title of the back bar button of the destination controller displays automatically the title of the navigation item of the source controller, a way is to set the title of the navigation item in the source view controller

  • in viewWillAppear() or viewDidAppear() to the normal title.
  • in prepareForSegue() to the title the back bar button is supposed to display.

This is the way I would do it

Create a new variable for your bar button at the top of the file:

var leftBarButton: UIBarButtonItem!

And then set the the attributes of the button

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

And finally set the new bar button in your nav controller

self.navigationItem.leftBarButtonItem = leftBarButton

Hope it helps!

This solution worked for me: (use this in your pushed ViewController's viewDidLoad or viewWillAppear )

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

The way you've outlined the segues, it doesn't appear that ViewController3 should have a back button at all. A Show Detail segue replaces the child view controller in a UISplitViewController or presents the child modally otherwise. There's nowhere to go back to.

Try this inside viewDidLoad on ViewController3 (using pre-Swift 2 syntax):

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

When I try that on a sample project, it's nil. I think that's the core problem.

This bit of hackery works:

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

However, that's pretty awful. Manipulating the title of the presenting ViewController (per @vadian's advice) is obviously cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM