Timering

Purp

Custom Segue, Unwind Segue with Animations & NavController - Part 2/2

In this tutorial, we continue the segue settings. Now we do custom segue with UIAnimation. This tutorial made in IOS 8 Xcode 6 Tutorial: Basic Xcode project. In the first part, we created UIViewControllers, with UIButtons. Embed UINavigationController, and manage Custom Segue and UnWind Segue. Part 2/2 Tutorial, using UIAnimation at Custom Segue.

Steps:

  1. Create Custom Segue class and “-(void)perform” code
  2. Change the Segue class on the storyboard
  3. Test your Custom Segue in IOS Simulator
  4. Create UnwindSegue 
  5. Detect MyCustomUnwindSegue on Storyboard
  6. Test your CustomUnwindSegue and add color to your window background

Parts of the series:

  1. Part:1/2: Basic Settings - Custom Segue, Unwind Segue with Animations & NavController

Video tutorial:

1. Create Custom Segue class and “-(void)perform” code

1.1. Right click New File — Cocoa Touch Class - name: MyCustomSegue, type: UIStoryboardSegue

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_1_1

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_1_2

1.2. in MyCustomSegue.m file create "-(void)perform" code:

You can refer to source VieController and destination ViewController, simply, and you can use the MyCustomSegue several places on the storyboard, regardless of which specific ViewController use it.

1.3. Put the animation into the "-(void)perform" code:

For example, just come to the destination ViewController from up to down, and source ViewController just goes down. So the entirely "perform" code looks like this:

Probably you will find these line there:

2. Change the Segue class on the storyboard

2.1. Click on the segue, and in the Attributes inspector change the Segue from "Show" to "Custom"

2.2. Add the MyCustomSegue class to the Segue.

 SpotlessiCode | IOS Tutorial | Custom Segue Part 2_2_2

3. Test your Custom Segue in IOS Simulator

Before MyCustomSegueClass: check in the video at this point

After MyCustomSegueClass: check in the video at this point

4. Create CustomUnwindSegue

4.1. Repeat the first Step, so add New File with MyCustomUnwindSegue name…

SpotlessiCode | IOS Tutorial | Custom Segue Part 2 4_1

Attention! - Changes:

4.3. the easiest way is to copy the code from here. :)

4.4. In ViewController.m file create the IBAction UIStoryboardSegue to handle the custom unwind segue, and add "if statement" to change the background to orangeColor:

Now you have 2 type of UnwindSegue in your code. The first is the normal UnwindSegue, and the second your own CustomUnwindSegue. So if you use CustomUnwindSegue, and would like to do some changes after segue animation, you can code here in returnedFromSegue.

Your code looks like this:

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_4_4

4.5. Add returnedFromSegue to your new UnwindSegue at your SecondViewController’s button.

Ctrl-drag to Exit and choose returnedFromSegue.

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_4_5_1

Click on the UnwindSegue, and add UnwindFromSecondView as Identifier.

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_4_5_2

Probably you will find these line there:

5. Detect MyCustomUnwindSegue on Storyboard

5.1. In ViewController.m : import MyCustomUnwindSegue.h file

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_5_1

5.2. segueForUnwindingToViewController:

If you not use NavigationController just put this segueForUnwindingToViewController:code in ViewController.m, this will be detect  which segue should be use MyCustomUnwindSegue class:

5.3. Subclass NavigationController

If you use NavigationController on your first ViewController (where comes the UnwindSegue), you should subclass your navcontroller:

5.3.1. Add New Cocoa Touch Class File

name: MyNavController, type: UINavigationController

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_5_3_1

5.3.2. On Storyboard add your new class to your NavigationController.

 SpotlessiCode | IOS Tutorial | Custom Segue Part 2 5_3_2

5.3.2. SegueForUnwindingToViewController:

In MyNavController.m file import MyCustomUnwindSegue.h file, and put the segueForUnwindingToViewController:code. So your MyNavController file looks like this:

6. Test your CustomUnwindSegue and add color to your window background

6.1. Play in IOS Simulator.

Check in video tutorial at this point.

If you would like to some other color instead of black window background, just put this color code in MyCusotmUnwindSegue.m file
window.backgroundColor = [UIColor redColor]; looks like this:

SpotlessiCode | IOS Tutorial | Custom Segue Part 2_6_1_"

6.2. And test again with red window background color:

Check this in Video tutorial at this point.

Download code

You can download the whole tutorial from github: MyCustomSegue.git

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on twitter! Check Footer section for these opportunities!