13

I'm trying to call function when i'm pressing default Back button (e.g. < Settings) in Navigation Controller.

At other situations i'm creating custom button, after i'm Ctrl+Dragging to red Exit button in XCode and pointing it to Selection segue.

Now i need to use default back button of navigation controller, but i can't set assign it to segue action because it's no back button in Storyboard view (but it exists in Simulator).

Simulator:

Simulator

XCode: XCode

I can create custom button in navigation contoller and assign it so custom unwind segue, but that button will be ugly without "< " symbol.

I've found some answers at stackoverflow, but they all are written in Objective-C :(.

Vasily
  • 3,740
  • 3
  • 27
  • 61
  • 1
    Did anybody figure out how do this by using a segue with the back button. I would rather do it that way than use UINavigationControllerDelegate if possible. – daniel Jul 13 '22 at 19:21

2 Answers2

1

You can use a delegate method to accomplish what you are doing.

Here is a really thought out swift tutorial that deal with delegates and segues: http://makeapppie.com/2014/07/05/using-delegates-and-segues-part-2-the-pizza-demo-app/

What you are asking, ability to catch the back button's action, isn't possible the way you describe it.

Most people would recommend creating a custom button, then use an unwind segue.

There are lots of tutorials on custom back buttons just in case you're new to swift, and the ugly part of the custom back button can be easily fixed by putting your own arrow there.

Also I found this previous asked question for future reference: Unwind segue from navigation back button in Swift

Community
  • 1
  • 1
YichenBman
  • 5,011
  • 8
  • 46
  • 65
  • 2
    Used delegate method, all is working. "Back" button i'm catching with viewWillDisappear at that class, and calling from viewWillDisappear my delegate method. I have only back option, so I can use viewWillDisappear. – Vasily Mar 24 '15 at 20:55
1

Actually I just assigned the unwind segue to the entire view controller (right click from the yellow view controller outlet and drag to the exit outlet then select the function). And it works great so far. No need custom button, no delegate. In fact, in my project the child is supposed to call the different parent's function according to current caller view controller and simply using the same function name made my dreams come true.

This page helped me a lot: http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/

smozgur
  • 1,772
  • 1
  • 15
  • 23
  • 3
    I tried to accomplish the same thing but I must be doing something wrong. Can you help me? In the 1st VC I declared the unwind IBAction. Then, in IB, I right click dragged from the 2nd view controller (yellow icon on the deck) to the exit icon (on the same deck), and I select my unwind IBAction from the 1st VC. But, it's not called when I tap on the Back navigation button on the 2nd VC. Any ideas? – maganap Aug 03 '16 at 09:39
  • @maganap: What I did was for making it work with performSegueWithIdentifier method programmatically (Yellow to Exit), I needed to call performSegueWithIdentifier in the object action. However, what you simply need is Right Click -> Drag from your button to Exit and select your action segue. See the sample I put on github: https://github.com/smozgur/Unwind-Segue – smozgur Aug 03 '16 at 11:51
  • 4
    oh, yes I can do that from an object. But, going along with @YichenBman's question, what I wanted to do is pass data back to VC1 when **the default back button** is tapped in VC2, which doesn't seem to allow its actions to be catched. I ended up triggering the segue in VC2 `viewWillDisappear`, which does fire the unwind action in VC1. But this is certainly not the correct way to do it, since navigation was already triggered, I shouldn't call a segue there. Thanks a lot for your help anyway :) – maganap Aug 04 '16 at 08:33
  • @maganap, you can instead use UINavigationControllerDelegate and set navigationController.delegate to be self in the VC2 viewDidLoad and pass data back to VC1 in navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) function. I updated my sample code - it might help. – smozgur Aug 04 '16 at 09:35