3

I want a navigationbar to have a back button which goes to the last page, even though the last page didn't have a navigation bar. How can i insert a back button, and control what happens when the user presses it?

Andrew
  • 15,935
  • 28
  • 121
  • 203

4 Answers4

1

I would like to add an answer, but unfortunately I only have the correct answer in Swift. I will provide it here for anyone who comes along this question looking for an answer in Swift. Let's suppose you would like to add a "Back Button" to your navigation bar to go to the last page. Do the following:

    let button = UIBarButtonItem.init()
    button.title = "BACK"
    let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 15.0)]
    button.setTitleTextAttributes(attributes, for: .normal)
    button.target = self
    button.action = #selector(self.goBack)
    self.navigationItem.setLeftBarButton(button, animated: false)

Then, define a function (that is used for the selector for the button's action above):

@objc func goBack() {
    self.performSegue(withIdentifier: "myUnwindSegue", sender: AnyObject.self) 
}

Note that here I make use of an unwind segue. To use an unwind segue, please refer to here or comment below if you have any questions. Unwind segues can be tricky when you first use them, but afterwards they are great. Also note that when you say "last page" I assume you mean a view already added to the view hierarchy.

Joshua Wolff
  • 2,687
  • 1
  • 25
  • 42
0

You need to link the button from Interface Builder to the method you want to be called when the button has been clicked on. The method should look like this - (IBAction)backAction. The name of the method is up to you but you have to declare the return type to IBAction so Interface builder is aware that a button can be linked to it.

grandouassou
  • 2,500
  • 3
  • 25
  • 60
0

A navigation bar isn't required for a view controller to be managed by a navigation controller. Rather than trying to fake a back button, use a navigation controller to manage both view controllers, and have the first controller hide the navigation bar. For example, you can add something like:

[self.navigationController setNavigationBarHidden:YES animated:YES];

to the controller's -viewDidAppear method. Do something similar for the second controller, passing in NO for the hidden parameter in order to display it again.

In the general case, though, a view controller can add a back button to the nav bar with code like this (warning: untested code typed from memory, but should get you started):

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:myBackImage style: UIBarButtonItemStylePlain target:self action:someAction];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

When the button is tapped, the button will send its action (someAction) to its target (self).

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Both views are in the same view controller though. – Andrew Mar 08 '11 at 01:29
  • You know that's not the way the framework is intended to be used, right? If you want to go off in your own direction with respect to your app's design, that's up to you -- I showed you how to set up your own button in a navigation item, and that should work regardless. But there's a strong expectation in Cocoa Touch that each view controller manages one "screen". Unless there's an important reason to do otherwise, disregarding that convention is setting yourself up for a world of unnecessary pain. – Caleb Mar 08 '11 at 02:00
  • @Caleb - just a note for anyone reading this, that's not the convention anymore – Sam Sep 09 '11 at 15:05
-2
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

[button.titleLabel setFont:[UIFont boldSystemFontOfSize:11.0]];

[button setBackgroundImage:[UIImage imageNamed:@"back_norm.png"] forState:UIControlStateNormal];

[button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateSelected];

[button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateHighlighted];

[button setTitle:@" Back" forState:UIControlStateNormal];

[button setTitle:@" Back" forState:UIControlStateSelected];

[button setTitle:@" Back" forState:UIControlStateHighlighted];

button.frame = CGRectMake(0, 0, 48, 30);

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationItem.leftBarButtonItem = back; 

[button release];

[back release];