- To create and use segue first of all we will require two viewcontrollers. And I have created two of them namely
ViewController
andSecondViewController
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
toSecondViewController
. - 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
toSecondViewController
- And thats all. If you run your app now click on the button it will take you to the
SecondViewController
-
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 theSecondViewController
and chose type of segue. Now this segue linksViewController
toSecondViewController
-
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)
- 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
-
Lets consider what user does on first screen will determine the what will apear on second screen.
-
Lets take
TextField
on first screenViewController
andLabel
on second screenSecondViewController
. 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
toViewController
and named ittextField
andLabel
toSecondViewController
and named itlabel
. -
Now override a method
prepare for segue
as belowoverride prepare (for segue: UIStoryboardSegue, sender: Any?){ if segue.indentifier == "goToSecondScreen" { } }
-
Now lets create a prperty in
SecondViewController
calledtextPassedOver
.var textPassedOver : String?
-
Now we have to grab the reference of
SecondViewController
and assign value to it fromtextField
let destinationVC - segue.destination as! SecondViewController destinationVC.textPassedOver textField.text
-
Now lets set
textPassedOver
value tolabel
inSecondViewController
. -
And its working.
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.