0

I'm studying Swift, I make course in DesignCode and now I'm trying to create an app to improve what I've learned.

So that is what I do:

  • I created UIViewController and perform a segue to a UITableViewController;

  • In the UITableViewController I embedded in with navigation bar where I create a 'Back' Bar Button Item;

  • And I link this 'Back' to exit, performing a segue to unwind via 'Spring'.

When I click on this button I receive the following error:

2015-06-04 15:32:08.662 Vitrine[2883:67901] *** Assertion failure in -[UIStoryboardUnwindSegueTemplate _perform:], /SourceCache/UIKit_Sim/UIKit-3347.44/UIStoryboardUnwindSegueTemplate.m:78
2015-06-04 15:32:08.676 Vitrine[2883:67901] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <UINavigationController: 0x7ae41540>'
*** First throw call stack:
(
    0   CoreFoundation                      0x0154d746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x02f4ea97 objc_exception_throw + 44
    2   CoreFoundation                      0x0154d5da +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x019c3720 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
    4   UIKit                               0x0253d3b4 -[UIStoryboardUnwindSegueTemplate _perform:] + 387
    5   UIKit                               0x0237c313 -[UIStoryboardSegueTemplate perform:] + 116
    6   libobjc.A.dylib                     0x02f647cd -[NSObject performSelector:withObject:withObject:] + 84
    7   UIKit                               0x01d72a90 -[UIApplication sendAction:to:from:forEvent:] + 99
    8   UIKit                               0x020f8bba -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    9   libobjc.A.dylib                     0x02f647cd -[NSObject performSelector:withObject:withObject:] + 84
    10  UIKit                               0x01d72a90 -[UIApplication sendAction:to:from:forEvent:] + 99
    11  UIKit                               0x01d72a22 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    12  UIKit                               0x01eb318a -[UIControl sendAction:to:forEvent:] + 69
    13  UIKit                               0x01eb35a7 -[UIControl _sendActionsForEvents:withEvent:] + 598
    14  UIKit                               0x01eb2811 -[UIControl touchesEnded:withEvent:] + 660
    15  UIKit                               0x01dcacfa -[UIWindow _sendTouchesForEvent:] + 874
    16  UIKit                               0x01dcb7d6 -[UIWindow sendEvent:] + 792
    17  UIKit                               0x01d896d1 -[UIApplication sendEvent:] + 242
    18  UIKit                               0x01d99b08 _UIApplicationHandleEventFromQueueEvent + 21484
    19  UIKit                               0x01d6d337 _UIApplicationHandleEventQueue + 2300
    20  CoreFoundation                      0x0146f06f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    21  CoreFoundation                      0x01464b7d __CFRunLoopDoSources0 + 253
    22  CoreFoundation                      0x014640d8 __CFRunLoopRun + 952
    23  CoreFoundation                      0x01463a5b CFRunLoopRunSpecific + 443
    24  CoreFoundation                      0x0146388b CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x050c62c9 GSEventRunModal + 192
    26  GraphicsServices                    0x050c6106 GSEventRun + 104
    27  UIKit                               0x01d71106 UIApplicationMain + 1526
    28  Vitrine                             0x00076cd4 main + 180
    29  libdyld.dylib                       0x0366bac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Regards, Diogo Amaral

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23

2 Answers2

0

To add standard Navigation and navigation bar you should use a UINavigationController. The UINavigationController will "wrap" the other views in the navigation flow (stack), and therfore you need not add any UINavigationBar on any views.

If implemented correctly Back is taken care of automagically by the UINavigationController.

enter image description here

Left arrow: Inital View Controller should point at the UINavigationController

1st segue: root view controller = First view controller that will show.

2nd segue: show. Trigger a show segue and the UINavigationController will push the next view. A back link will show for back navigation.

You can chain as many views as you need using the show segue, and you can have many show segues from one and the same view controller if needed.

UPDATE:

And if you really want to use the unwind segues for different reasons, here is an excellent explanation on how they work:

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Mikael Hellman
  • 2,664
  • 14
  • 22
  • Thank you, it works but... If want just present a modal, how I can do that? Because doing in this way, the Navigation always appears. Regards, Diogo Amaral – Diogo Amaral Jun 04 '15 at 19:29
  • There is a `segue` called `present modally`. It will push a view controller "over" the current view controller. (Default animation, flying in from the bottom). Try it out, it's awesome. :) – Mikael Hellman Jun 04 '15 at 19:33
  • And if I misunderstood the question, this would be how to dismiss it after you get on the screen: http://jeffreysambells.com/2014/02/19/dismissing-a-modal-view-using-a-storyboard-segue – Mikael Hellman Jun 04 '15 at 19:36
  • I already insert a segue `present modally` but when I change the `presentation` of `default` to `over current context` the unwind does not work. – Diogo Amaral Jun 04 '15 at 20:18
  • The way that @Gabriel Bitencourt gave me works perfectly, thank you man! – Diogo Amaral Jun 04 '15 at 20:26
0

You have to, in the view you wanna get back for, create an IBAction like this:

@IBAction func back(segue:UIStoryboardSegue) {
        //no code here, unless you want to do something when exiting
    }

And then, in the view you want to exit from, link your barButtonItem to the exit (the IBAction should appear there).