1

Basically I have a view controller that pushes to the next view controller with [self performSegueWithIdentifier: sender:]; and in the prepareForSegue I wanted to change the VC's title to "Back" so that in the next VC the back bar button item will be "Back" instead of the initial VC's title.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    self.title = @"Back";

}

enter image description here

Now the problem is that when I return back to the first VC by tapping on the Back button, I have to change the title back to the initial title or else the title will stay as "Back".

If only there was a method like prepareForSegue so that I can get the destination VC and change it's title...

Another example is that I have a storyboard of 3 view controllers like so:

enter image description here

I've hidden the navigation controller to use my own button to go back the segue. While this way works, but it seems rather odd that I have to add 2 more segues just to return back to the first VC...

iamhx
  • 472
  • 6
  • 24

2 Answers2

1

In your storyboard, drag a Navigation Item onto your first view controller. Set the Title to your view controller's title, and Back Button To "Back"

enter image description here

Alternatively, to do this programatically, create a UIBarButtonItem with nil action and target, and assign to navigationItem.backBarButtonItem (make sure you do this in the view controller you are coming from)

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Brilliant! I was searching all over stackoverflow and tried answers like "do this in `viewDidLoad` etc." and I can't believe it was THAT simple. One question however, is there a way I can do an action (Like alert "Are you sure to go back?") when the back button is tapped? – iamhx Feb 18 '17 at 09:24
  • Unfortunately, there's no way to intercept the back button. The closest you'll get is adding a custom `leftBarButtonItem` to the child view controller, hooking up to an `IBAction`, then presenting the alert from there – Ashley Mills Feb 18 '17 at 09:49
  • Yeah, you're right. Saw this question here and did the button implementation instead.. Much thanks anyway! http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios – iamhx Feb 18 '17 at 09:56
-1

in the methodprepareForSegue:, you can get the destinationViewController by segue.destinationViewController. i think this is what you want.

Mistrx丶
  • 267
  • 3
  • 10
  • Nope, it doesn't work. Theres no segue indicating to go back to the previous VC. I tried using the same segue (The one that navigates forward) and when I tapped on the back, it's not firing up my action. `if ([segue.identifier isEqualToString:@"oneMinChallenge"]) {//action}` – iamhx Feb 18 '17 at 09:33