Giter Site home page Giter Site logo

architectak / creating-and-using-segue-ios Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 27 KB

Creating And Using Segue in Xcode - The Intent for iOS

Swift 100.00%
segue segues seguewithcompletion viewcontroller-transition viewcontroller-transitions

creating-and-using-segue-ios's Introduction

Creating And Using Segue in Xcode

Creating a segue from UI element to another view controller

  • To create and use segue first of all we will require two viewcontrollers. And I have created two of them namely ViewController and SecondViewController and both are there in storyboard as well.
  • Lets say in our first viewcontroller we add a button from object directory by dragging and dropping uibutton in the viewcontroller, this button will take us from ViewController to SecondViewController.
  • In order to that we need to setup a segue and setting up a segue is very easy. All you need to do is just click the button, hold down the control key on keyboard and click and drag and point to the SecondViewController.
  • You will see a popup of types of segue, chose Show from the options. It can be change at point of development time if needed later on.
  • As soon as you chose Show from the option, you will see that Xcode has drawn a line from ViewController to SecondViewController
  • And thats all. If you run your app now click on the button it will take you to the SecondViewController

Segue can be created from one view controller to another without initiating it from any UI Element

  • What you need to just click on yellow symbol on top the screen which indicates the "View Controller"

  • Hold down the control key and drag aero to the SecondViewController and chose type of segue. Now this segue links ViewController to SecondViewController

  • Now your question might be "how do I trigger it when I press the button ? "

  • In order to use this segue you will need to give it a identifier, go to atribute inspector and keep segue selected and give a name to it. I have given goToSecondScreen.

  • Now we need to link the button via IBAction, we can call it buttonPressed.

  • Now all we have to say that

      performSegue(withIdentifie: "goToSecondScreen", sender : self)
    

Why need identifier for segues

  • Actually we can create multiple segues from single view controller to multiple view controllers.
  • We can trigger the other segue from other button on single view controller to some other view controller.
  • Since identifier is the string, we have to make sure that we use it exaclty with casing to completely identical to how we names it in atribute inspector

Passing data from one view controller to another using a segue

  • Lets consider what user does on first screen will determine the what will apear on second screen.

  • Lets take TextField on first screen ViewController and Label on second screen SecondViewController. We are going to ask user to enter some data on first screen and we will pass it to the second screen to display as result.

  • I have connected TextField to ViewController and named it textField and Label to SecondViewController and named it label.

  • Now override a method prepare for segue as below

      override prepare (for segue: UIStoryboardSegue, sender: Any?){
          if segue.indentifier == "goToSecondScreen" {
      
          }
      }
    
  • Now lets create a prperty in SecondViewController called textPassedOver.

          var textPassedOver : String?
    
  • Now we have to grab the reference of SecondViewController and assign value to it from textField

          let destinationVC - segue.destination as! SecondViewController
          destinationVC.textPassedOver textField.text
    
  • Now lets set textPassedOver value to label in SecondViewController.

  • And its working.

Contact - Let's connect to learn together

License

Copyright 2018 Ankit Kumar

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

creating-and-using-segue-ios's People

Watchers

 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.