【swift学习笔记】五.使用枚举优雅的管理Segue

简介:

在做页面转跳的时候,我们要给Segue命名,如果Segue多了,管理他们就是一个恶梦。我们可以枚举更优雅的管理这些Segue。

  1.我们先来建立一个protocol,他的功能就是让实现类实现一个SegueIdentifier别名,这个SegueIdentifier必需为RawRepresentable类型,在后边我们就会用

protocol SegueHandlerType {
    associatedtype SegueIdentifier: RawRepresentable
}

  2.我们要对上边的protocol扩展,并且实现protocol必须为UIViewControl,SegueIdentifier的原值要为String类型

这个扩展有两个方法一个是用要实现的别名来调用执行Segue方法performSegueWithIdentifier。另一个方法segueIdentifierForSegue通过Segue的identifier来得到SegueIdentifier.

复制代码
extension SegueHandlerType where Self: UIViewController, SegueIdentifier.RawValue == String {
    func performSegueWithIdentifier(segueIdentifier: SegueIdentifier, sender: AnyObject) {
        performSegueWithIdentifier(segueIdentifier.rawValue, sender: sender)
    }
    
    func segueIdentifierForSegue(segue: UIStoryboardSegue) -> SegueIdentifier {
        guard let identifier = segue.identifier,
            segueIdentifier = SegueIdentifier(rawValue: identifier)
        else {
            fatalError("invalid segue identifier \(segue.identifier)")
        }
        return segueIdentifier
    }
}
复制代码

 

  3.用ViewController实现SegueHandlerType。再用一个枚举来实现SegueIdentifier并且为String,里面有两个case这两个就要我们要打开的新窗体的名称。

下边有两个按钮Action是用SegueIdentifier枚举来打开相应的窗体。

复制代码
class ViewController: UIViewController, SegueHandlerType {

    enum SegueIdentifier: String {
        case ShowView1
        case ShowView2
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func openView1(sender: AnyObject) {
        performSegueWithIdentifier(.ShowView1, sender: self)
    }
    @IBAction func openView2(sender: AnyObject) {
        performSegueWithIdentifier(.ShowView2, sender: self)
    }
}
复制代码

 

  4.新建两个新窗体,分别用Segue联接这两个窗体。这两个Segue分别命名为我们的枚举类型的两个值:“ShowView1”  “ShowView2”。再来两个Button连接后台的两个窗体的Action

 

 

 

源代码:segueTypeDemo.zip


本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/p/5561149.html,如需转载请自行联系原作者

相关文章
|
4月前
|
数据采集 Web App开发 Swift
提高数据抓取效率:Swift中Crawler的并发管理
提高数据抓取效率:Swift中Crawler的并发管理
|
5月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
261 2
|
5月前
|
Swift
Swift 中的枚举(Enum)
Swift 中的枚举(Enum)
42 2
|
存储 编译器 Swift
Swift-进阶 08:枚举enum
Swift-进阶 08:枚举enum
609 1
Swift-进阶 08:枚举enum
|
Swift
Swift实用小册07:枚举的创建、使用、遍历、关联值、原始值
Swift实用小册07:枚举的创建、使用、遍历、关联值、原始值
391 0
Swift实用小册07:枚举的创建、使用、遍历、关联值、原始值
Swift - Enum枚举 源码分析
Swift - Enum枚举 源码分析
 Swift - Enum枚举 源码分析
|
Swift
Swift学习笔记——页面跳转
创建一个single view app后,项目中有main.storyboard,里面是一个viewcontroller。 那么我们如何实现页面跳转 首先添加一个导航控制器Navigation Controller。点击右上的➕,然后选择控件拖到面板上即可
1226 0
|
前端开发 测试技术 Swift
Swift学习笔记——新建项目
在xcode菜单中选择 new -> project -> single view app 点击next在弹出窗中填写项目名称 这里languge有可以选择object-c或swift作为项目语言。这里我们学习swift,所以选择swift。 如果language选择swift,下面的user interface可以选择swiftUI和storyboard。 SwiftUI是2019 年 WWDC 大会上,苹果在压轴环节向大众宣布了基于 Swift 语言构建的全新 UI 框架,与flutter类似,是用代码编写页面,支持快速预览。
720 0
|
存储 编译器
Swift5.1—递归枚举
Swift5.1—递归枚举
151 0
|
存储 安全 Swift
Swift5.1—枚举
Swift5.1—枚举
118 0