Connect one UIButton to multiple view controllers iOS Swift

This tutorial has been updated for X code 7.3.1 & swift 2.3

When app is downloaded for first time you want user to add some initial information before they can start using the app. But lots of app immediately prompt user to log in or create an account. And they block user from doing anything in the app before they log in. This will get you higher sign up rate but this creates a bad user experience. You want the user to play around with the app & when the user is good and ready then you want to prompt user to create an account or log in.

So we are going to solve problem by connecting two view controllers to single button. If no account exist then user will be showed create account view controller. If account exists then user can start using the app.

1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to one button two segue & save it on desktop.

new X code project

2. Go to main storyboard, select view controller & go to Editor -> Embed In -> Navigation Controller. Then set navigation item title to Segue A.

add navigation controller

3. Add another view controller to main storyboard, select view controller & go to Editor -> Embed In -> Navigation Controller. Then set navigation item title to Segue B.

add view controller & navigation controller segue B

4. Add another view controller to main storyboard, select view controller & go to Editor -> Embed In -> Navigation Controller. Then set navigation item title to Segue C.

add view controller & navigation controller segue C

Screen Shot 2016-06-24 at 1.45.38 PM

5. Connect Segue A to Segue B and Segue C by Modal Segue as follows

modal segue

6. Select model segue between Segue A and Segue B and set its identifier to segueAToB as follows

segue A to B

7. Select model segue between Segue A and Segue C and set its identifier to segueAToC as follows

segue A to C

8. Add bar button item to Segue A & set its title to Segue B.

segue B button

9. Open assistant editor and connect add button to view controller as action as follows

bar button as action

10. Create variable in view controller as follows

class ViewController: UIViewController {
// 10
var shouldOpenSegueB = true

11. Add following code in add pressed action

@IBAction func addPressed(sender: UIBarButtonItem) {
    // 1
    if shouldOpenSegueB {
        // 2
        sender.title = "Segue C"
        // 3
        shouldOpenSegueB = false
        // 4
        performSegueWithIdentifier("segueAToB", sender: self)
    }
    else {
        // 5
        sender.title = "Segue B"
        // 6
        shouldOpenSegueB = true
        // 7
        performSegueWithIdentifier("segueAToC", sender: self)
    }
}
  1. If the bool is true then execute following code
  2. Set button title to Segue C
  3. Change bool value to false
  4. Open Segue B view controller
  5. If bool is false then set its title to Segue B
  6. Set bool to true
  7. Open Segue C view controller

12.  Create unwind segue in view controller as follows

class ViewController: UIViewController {
// 12
@IBAction func cancelToSegueA(segue: UIStoryboardSegue) {

}

13. Go to main storyboard, add bar button item to Segue B & set its system item to Cancel

cancel button to segue B

14. Connect cancel button to cancel unwind segue as follows

bL9hKwgp9W

15. Add bar button item to Segue C & set its system item to Cancel

cancel button to segue c

16. Connect cancel button to cancel unwind segue as follows

oARzYZMAQf

17. Now build and run app in iPhone 5s.

LlrTaRoUw2
Download X code file
Challenge : Add textfield to segue B & if textfield is empty then stop the segue from happening & give warning to user

scRpUwV2IW
Download Challenge Solution

Total 1 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?