32

Is it possible in iOS 6 to know when a UIStoryboardSegue has finished its transition? Like when i add a UIStoryboardSegue from UIButton to push another UIViewController on the navigationcontroler, i want to to something right after the push-transition is finished.

bogen
  • 9,954
  • 9
  • 50
  • 89

5 Answers5

20

You can use the UINavigationControllerDelegate protocol and then define:

– navigationController:didShowViewController:animated:
Warpling
  • 2,024
  • 2
  • 22
  • 38
New
  • 216
  • 1
  • 2
  • Wasn't as bad as I expected. `self.navigationController = self;` and add that method. I saved the controllers into local variables based on the Segue ID and then compare those values to the passed `ViewController`. – Brian White Jan 13 '15 at 21:11
  • 9
    This will work for push segues where the originating view controller has a navigation controller, but what to do in the case of modal segues? – Drux Feb 10 '15 at 08:13
  • 1
    I used the custom Segue answer from Levi and implemented my `perform` like the example here: http://stackoverflow.com/a/23778394/586489 This works quite nicely, I know when the push is completed and can use segues. – Mac_Cain13 Apr 13 '15 at 19:30
  • True enough, though sadly no use to me as I'm using a custom parent view controller, not a navigation controller. – Ash Aug 23 '15 at 10:28
  • 1
    if you're using iOS9 you can try this answer to execute code in a completion block after a Segue finishes it's `perform()` http://stackoverflow.com/a/37602422/1552116 – wyu Jul 05 '16 at 16:05
15

In case you don't want to use the viewDidAppear: method, you could create a custom segue. In the perform method you would use an animation for the transition, and that can have a completion block. You can add the code there after the animation is complete.

Levi
  • 7,313
  • 2
  • 32
  • 44
  • So it's not possible with the standard drag-and-drop segue in Storyboard? – bogen Aug 19 '13 at 09:30
  • 2
    It is how you create it, but you have to subclass `UIStoryboardSegue` and implement the `perform` method in it. After that, in `storyboard` you set the class of the segue to the one you just created. – Levi Aug 19 '13 at 09:54
8

In Swift, from a UIViewController subclass you can get the UINavigationController instance and set the delegate, in order to be informed about the completion of segues, as shown. Another logical place to track segues might be the AppDelegate.

Example of doing it from a view controller (VC for short):

class MyViewControllerSubclass : UIViewController, UINavigationControllerDelegate {

    func viewDidLoad() {
        self.navigationController.delegate = self
    }

    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        println("Did show VC: \(viewController)")
    }
 }

But that only shows you when the segue to the VC is complete, as would viewWillAppear() or viewDidAppear() delegate methods in the VC being presented; however, they don't inform about when the target VC is un-presented. It will also only work if your View Controller is part of a Navigation Controller stack.

In the VC you're tracking, you could add the following to detect when the VC (and its memory) are deallocated, or override the viewWillDisappear() method.

deinit {
    println(__FUNCTION__, "\(self)")
}
nitro805
  • 33
  • 5
clearlight
  • 12,255
  • 11
  • 57
  • 75
-10

You can use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

This method will be called right before a segue is performed in the source UIViewController. If you want to do some code in the destination UIViewController you can get the destination viewcontroller of segue.

You can also add this code in the viewdidAppear in the desintation viewController.

ahmedalkaff
  • 313
  • 1
  • 3
-11

you can call a method of destination UIViewController in prepareForSegue method.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  NSLog(@"prepareForSegue: %@", segue.identifier);

  if ([segue.identifier isEqualToString:@"Happy"]) {
      [segue.destinationViewController setHappiness:100];
  } else if ([segue.identifier isEqualToString:@"Sad"]) {
      [segue.destinationViewController setHappiness:0];
  }
}

here setHappiness method is of destination Controller and here 100 is passing there. so you can write a method in destination controller and call it here

  • 4
    That's not really indicating when the actual segue is complete though- this happens at the start of the segue – Sam Apr 30 '14 at 15:25