0

I am building a single view application for iOS in Xcode with its own navigation system (I'm not using the built in "navigation controller") but there are buttons throughout that link to different pages of information. I'm trying to make a button that takes you back one page. A button that functions like the back button does when you use the navigation controller or the back button in a browser.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What kind of navigation are you using, if not UINavigationController? We would need to know that before providing a proper answer. – Tim B. Dec 23 '15 at 22:13
  • try this: http://stackoverflow.com/questions/5217992/back-button-callback-in-navigationcontroller-in-ios – piotrek1543 Dec 23 '15 at 22:15

3 Answers3

0

If you're using a UINavigationController:

[navigationController popViewControllerAnimated:YES];

Otherwise:

[viewController dismissViewControllerAnimated:YES completion:nil];
Jared Price
  • 5,217
  • 7
  • 44
  • 74
  • Thanks for the response, I'm gonna ask a noob question though, I am using viewController, not navigationController. So where would the second piece of code fit in to my viewController file, currently I haven't put anything in there. All it has is the default code it starts with. And would I link that code with a button I put in to the main storyboard? – mynameisgav Dec 23 '15 at 22:23
  • You should be able to call `dismissViewControllerAnimated` within your `UIViewController` inheriting class. So find the code that runs when you push your button and try `[self.dismissViewControllerAnimated:YES completion:nil];`. I'm a bit rusty on my Objective-C but I think that's what you want. – Jared Price Dec 23 '15 at 22:26
0

Are your "links" done via segues in an Interface Builder storyboard?

Then just add unwind segues: - in the destination view controller, add:

- (IBAction)back:(UIStoryboardSegue *)segue
{
    // do anything you want here, including nothing
}
  • in the storyboard, Ctrl-drag from your button to the "Exit" icon (third icon at the top of the View Controller scene), and select back:

If you're linking programmatically, you're probably using presentViewController:animated:completion:, the opposite of which is dismissViewControllerAnimated:completion:

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Yeah I'm just linking everything from the storyboard with segues. So, if I put that piece of code in the destination view controller, ill be able to make a segue between the back button and the exit icon at the top of the view controller? – mynameisgav Dec 23 '15 at 23:11
  • Yes (with "destination view controller" being the one you go back to). Ctrl-Dragging from a button to the Exit icon will show all methods with that signature (`(IBAction)whatever:(UIStoryboardSegue *)whatever`) from view controllers that link directly or indirectly to the current view controller (you can go back several steps at once if needed). – jcaron Dec 24 '15 at 07:53
-1

If you need to go to one viewController down then you can use

[self.navigationController popViewControllerAnimated:YES];

Every controller contains property of navigationController so you can write it at every back button action.

EDIT: For button action you need to add IBAction from storyboard into controller.m and in that function you can write this code.

Krishna Kumar
  • 1,652
  • 9
  • 17
  • Not yet. Still trying to figure out the right place to enter code for this button. I've got my button placed in my view controller, but don't understand where exactly to write the code that will be attached to that button. – mynameisgav Dec 23 '15 at 22:51
  • https://developer.apple.com/library/ios/recipes/xcode_help-IB_connections/chapters/CreatingAction.html – Krishna Kumar Dec 23 '15 at 22:53
  • @mynameisgav DId above link helped you? – Krishna Kumar Dec 23 '15 at 23:09