What are Segues And Their Use In iOS Swift
Posted By : Varun Wadhwa | 30-Jun-2017
Segues: In iOS development segues use to define transition from one view controller to other view controller in storyboard file.Any clickable or gesture recognisable element can be source of segue and a view controller that we want to display will be the source of segue. so basically segue present a view controller but we can also use a segue to dismiss in case of unwind segue.
Creating a Segue : For creating a segue click on source (i.e from button , gesture recogniser or view controller) and right click + drag to destination view controller. There can be multiple segues from one view controller and similarly there can be multiple segues to a single view controllers. To handle multiple segues we can assign unique segue identifiers to segues.
Types of Segues : There are five types of pre-defined segues we can use , although we can define our own custom segues too.
a) Show (or Push) : This type of segue present the view controller over the source view controller. e.g.. In case of navigation controllers It pushes the view controller in navigation stack.
b) Show Details : It is used with UISplitViewController , It replaces a view controller with other view controller i.e like showing the details of selected content.
c) Present Modally : Displays the new view controller modally.
d) Present as Popover : It present a view controller as a popover, In case of iPhones it behave as Persent Modally segues , but in case of iPads the content displayed as popover.
Passing Data : We can pass data between view controllers on transitions. To fire a segue programatically we have to call a method
self.performSegue(withIdentifier: “segueIdentifier”, sender: self)
before transition we can override prepare method and an instance of view controller can be used to set values
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueIdentifier"
var vc = segue.destinationViewController as! DestinationViewController
vc.data = "Data to pass"
}
}
Custom Segues: We can implement custom segues as well and define our own transitions and behaviour too. All we need to do is to create a UIStoryboardSegue subclass and implement own perform method where we define the behaviour.
Thanks,
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Wadhwa
Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.