Giter Club home page Giter Club logo

step2.2-codeandseguereview's Introduction

step2.2-codeAndSegueReview

Exploring segue from storyboard to code and presenting view controllers image

Code Only (Present ViewControllers Programmatically)

@IBAction func rollTheDice() {

    // Get the DiceViewController
    let controller: DiceViewController
    controller = storyboard?.instantiateViewController(withIdentifier: "DiceViewController") as! DiceViewController

    // Set the two values to random numbers from 1 to 6
    controller.firstValue = randomDiceValue()
    controller.secondValue = randomDiceValue()

    // Present the view Controller
    present(controller, animated: true, completion: nil)
}

^ Above sets up new ViewController, passes data AND presents new ViewController

Code + Segue

image

image

Trigger segue with @IBAction func rollDice() on button with code -> `self.performSegue(withIdentifier: "rollDice", sender: self)`

Above only sets up the Segue and New ViewController but does not connect two ViewControllers

Segue (No Code)

image

Notice that:

In the RollViewController file, the rollTheDice method is empty.

In the Storyboard file, a segue connects the rollTheDice Button directly to the DiceViewController.

In the attributes inspector, the segue is given the identifier, “rollDice”.

Again, this only sets up the Segue and hides the instantiation of the new ViewController behind the story board, but does not pass data.

Passing data when using the 2nd and 3rd example (Code + Segue, and Segue)

image

We need to override the func prepare(for segue: UIStoryboardSegue, sender: Any?) method

We then cast DiceViewController as the segue.destination and then can pass the data to the ViewController that presents the dice.

If more than one segue attached to a ViewController

image

Use if segue.identifier == "nameOfSegue" { when we have more than one segue attached to a single ViewController

step2.2-codeandseguereview's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.