Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwind segue function with Back button

Tags:

ios

swift

segue

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 :(.

like image 473
Vasily Avatar asked Mar 24 '15 15:03

Vasily


2 Answers

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

like image 120
YichenBman Avatar answered Sep 22 '22 05:09

YichenBman


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/

like image 29
smozgur Avatar answered Sep 20 '22 05:09

smozgur