In doing page jump, we have to give segue name, if Segue more, manage them is a nightmare. We can enumerate more gracefully to manage these segue.
1. Let's start with a protocol, his function is to let the implementation class implement a Segueidentifier alias, this segueidentifier must be the rawrepresentable type, in the back we will use
Protocol Seguehandlertype { Associatedtype segueidentifier:rawrepresentable}
2. We want to extend the protocol to the top, and implement the protocol must be uiviewcontrol,segueidentifier the original value to be string type
There are two methods for this extension, one of which is to invoke the Execute Segue method Performseguewithidentifier with the alias you want to implement. Another method Segueidentifierforsegue through Segue's identifier to get segueidentifier.
Extension SeguehandlertypewhereSelf:uiviewcontroller, Segueidentifier.rawvalue = =String {func performseguewithidentifier (Segueidentifier:segueidentifier, sender:anyobject) {Performsegu Ewithidentifier (Segueidentifier.rawvalue, Sender:sender)} func segueidentifierforsegue (Segue:uistoryboardsegu e)-Segueidentifier {guard let identifier=Segue.identifier, Segueidentifier=segueidentifier (rawvalue:identifier)Else{fatalerror ("Invalid segue identifier \ (Segue.identifier)") } returnSegueidentifier}}
3. Implement Seguehandlertype with Viewcontroller. Using an enumeration to implement Segueidentifier and a string, there are two case, which is the name of the new form that we want to open.
There are two buttons below the action is to open the corresponding form with the Segueidentifier enumeration.
classViewcontroller:uiviewcontroller, Seguehandlertype {enumsegueidentifier:string { CaseShowView1 CaseShowView2}Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib. } Overridefunc didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.} @IBAction func OpenView1 (sender:anyobject) {performseguewithidentifier (. ShowView1, Sender:self)} @IBAction func OpenView2 (sender:anyobject) {performseguewithidentifier (. ShowView2, Sender:self)}}
4. Create two new forms, using Segue to join the two forms. These two segue are named as two values of our enumeration type: "ShowView1" "ShowView2". Two more buttons to connect the two form's action to the background
Source code:seguetypedemo.zip
"Swift Learning Notes" five. Using enumerations Elegant management segue