Custom UIStoryboardSegue using segueWithIdentifier:source:destination:performHandler:

I am trying to use a custom UIStoryboardSegue to implement a transition between two view controllers. I can do this by subclassing UIStoryboardSegue, and then setting this class in IB. However, I was looking at the docs which say:

If your segue does not need to store additional information or provide anything other than a perform method, consider using the segueWithIdentifier:source:destination:performHandler: method instead.

Implying that you don't need to create the custom subclass, just use the custom performHandler.

I am confused as to where this code should go, and how I go about using it. Do I create the segue as normal in IB and then override that before it is fired (maybe in shouldPerformSegue: or similar). Elsewhere in apple's documentation it says:

Your app never creates segue objects directly; they are always created on your behalf by iOS when a segue is triggered

So I don't quite understand why they are then saying to instantiate a segue using a class creator method.


The point of segueWithIdentifier:source:destination:performHandler:

  • Provide an alternative to UIViewController performSegueWithIdentifier:sender in cases where you also want to create a custom transition, without creating a segue subclass.
  • Vend a segue that can be used as the return for segueForUnwindingToViewController:fromViewController:identifier

As noted above, this approach is only viable for segues which you would call manually -- i.e. not for segues that would otherwise be triggered via IB triggers.

So, for example, if you have a segue that needs to be triggered after a certain timeout period (such as a custom lock-screen), you could use segueWithIdentifier:source:destination:performHandler: to handle the custom transition.

    UIStoryboardSegue *segue = 
                [UIStoryboardSegue segueWithIdentifier:@"LockScreenSegue" 
                     // transition code that would 
                     // normally go in the perform method
    // Dev is responsible for calling prepareForSegue and perform.
    // Note, the order of calls for an IB triggered segue as well as
    // a performSegueWithIdentifier segue is perform first, then
    // prepareForSegue:sender. Manual segues need to inverse the call
    // in order to ensure VC setup is finished before transition.
    [self prepareForSegue:segue sender:self];
    [segue perform];

Another practical use for the method is unwinding segues. Using a similar scenario to the previous example, we could use it to return a segue to transition from a lock screen back to the previous viewController:

-(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController*)toVC 
                                     fromViewController:(UIViewController *)fmVC
                                             identifier:(NSString *)identifier
    UIStoryboardSegue *segue = 
            [UIStoryboardSegue segueWithIdentifier:@"FromLockScreenSegue" 
                // transition code

    return segue;

If you wish to add functionality, just override the "prepareForSegue" method.

Don't forget to create a segue in your storyboard interface builder, then change it to custom, and name it under indentifier & storyboard id.

You will need to set the destination view controller, and you will be able to set the transition when using

[self presentViewController:YOUR_DESTINATION_VIEW_CONTROLLER animated:YES completion:nil];

, after creating an object of your destination view controller .

(Sorry for the short explanation, this is written with my iPhone, feel free to contact me with any further question).

Need Your Help

How to update two MySQL tables with PHP?

php mysql database

I Have 2 tables in my db that each have a column named Catergory.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.