"Swift Learning Notes" five. Using enumerations Elegant management segue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.