Swift-界面跳转

随着业务增加,项目中的模块越来越多,并且这些模块进行相互的调用,使得它们交缠在一起,增加了维护成本,并且会降低开发效率。此时就需要对整个项目进行模块划分,将这些模块划分给多个开发人员(组)进行维护,然后在主工程中对这些模块进行调用。

每个模块独立存在,提供接口供其他模块调用。从而如何有效且解耦的进行模块间的调用成了重中之重。

那么如何传递参数并且初始化对应模块的主窗口成为了主要问题。下面会介绍两种方案来解决这个问题。

方案1

得益于Swift中枚举可以传参的特性,我们可以通过枚举来进行参数传递,然后添加方法来映射控制器。

在枚举中通过参数来确定初始化控制器的数据,外部进行调用时可以很明确的知道需要传入哪些参数,同时还能传递非常规参数(DataUIImage等)

enum Scene {

    case targetA
    case targetB(data: Data)

    func transToViewController() -> UIViewController  {
        switch self {
        case .targetA:
            return ViewControllerA()
        case let .targetB(data):
            return ViewControllerB(data: data)
        }
    }

}

解决了UIViewController映射的问题后,我们接下来解决如何统一跳转方法。

项目中,基本上都会使用UINavigationController来进行界面的跳转,主要涉及pushpop操作。此时简便的做法是扩展UIViewController为其添加统一界面跳转方法。

extension UIViewController {
    
    func push(to scene: Scene, animated: Bool = true) {
        let scene = scene.transToViewController()
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.pushViewController(scene, animated: animated)
        }
    }
    
    func pop(toRoot: Bool = false, animated: Bool = true) {
        DispatchQueue.main.async { [weak self] in
            if toRoot {
                self?.navigationController?.popToRootViewController(animated: animated)
            } else {
                self?.navigationController?.popViewController(animated: animated)
            }
        }
    }

最后我们跳转界面时进行如下调用即可

push(to: .targetA)

push(to: .targetB(data: Data()))

面向协议进行改造

protocol Scene: UIViewController {
    
}

protocol SceneAdpater {
    
    func transToScene() -> Scene
}

protocol Navigable: UIViewController {
    
    func push(to scene: SceneAdpater, animated: Bool)

    func pop(toRoot: Bool, animated: Bool)
}
extension Navigable {
    
    func push(to scene: SceneAdpater, animated: Bool = true) {
        let scene = scene.transToScene()
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.pushViewController(scene, animated: animated)
        }
    }
    
    func pop(toRoot: Bool = false, animated: Bool = true) {
        DispatchQueue.main.async { [weak self] in
            if toRoot {
                self?.navigationController?.popToRootViewController(animated: animated)
            } else {
                self?.navigationController?.popViewController(animated: animated)
            }
        }
    }
    
}

经过以上面向协议改造,我们在使用时的代码如下:

enum TestScene: SceneAdpater {

    case targetA
    case targetB(data: Data)

    func transToScene() -> Scene {
        switch self {
        case .targetA:
            return ViewControllerA()
        case let .targetB(data):
            return ViewControllerB(data: data)
        }
    }

}

class ViewControllerA: UIViewController, Navigable, Scene {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        push(to: TestScene.targetB(data: Data()))
    }
    
}

class ViewControllerB: UIViewController, Scene {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    init(data: Data) {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

方案2

初始化UIViewController存在两种情况:需要传参、不需要传参。不需要传参的比较简单,直接调用UIKit提供的初始化方法即可。而需要传参的UIViewController,就涉及到如何确定传参的类型、传入哪些参数。此时需要利用协议的关联类型来确定传入的参数。

基于上述结论,制定的协议如下:

protocol Scene: UIViewController {
    
}

protocol NeedInputScene: Scene {
    
    associatedtype Input
    
    init(input: Input)
}

protocol NoneInputScene: Scene {
    
}

由此在跳转方法中需要用到泛型

protocol Navigable: UIViewController {
    
    func push<S: NeedInputScene>(to scene: S.Type, input: S.Input, animated: Bool)
    
    func push<S: NoneInputScene>(to scene: S.Type, animated: Bool)
}

extension Navigable {
    
    func push<S: NeedInputScene>(to scene: S.Type, input: S.Input, animated: Bool = true) {
        let scene = scene.init(input: input)
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.pushViewController(scene, animated: animated)
        }
    }
    
    func push<S: NoneInputScene>(to scene: S.Type, animated: Bool = true) {
        let scene = scene.init()
        DispatchQueue.main.async { [weak self] in
            self?.navigationController?.pushViewController(scene, animated: animated)
        }
    }
}

使用示例:

class ViewControllerA: UIViewController, Navigable {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        push(to: ViewControllerB.self, input: Data())
        
        push(to: ViewControllerC.self)
    }
    
}

class ViewControllerB: UIViewController, NeedInputScene {
    
    typealias Input = Data

    required init(input: Data) {
        super.init(nibName: nil, bundle: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ViewControllerC: UIViewController, NoneInputScene {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

方案2相较于方案1最显著的区别就是不再需要维护映射UIViewController的枚举类型。

使用枚举来作为入参,外部在调用时可以很清晰的确定需要传入参数的意义。而关联类型则不具备这种优势,不过这个问题通过使用枚举作为关联类型来解决,但是在UIViewController仅需要一个字符串类型时这种做法就显得有点重。

方案2在模块需要提供多个入口时,需要暴露出多个控制器的类型,增加了耦合。而方案1则仅需用暴露出枚举类型即可。

Demo

Demo对方案1进行了演示,也是我目前在项目中使用的方案。
更多路由相关信息请查看一下参考链接

参考连接:

https://github.com/meili/MGJRouter

https://casatwy.com/iOS-Modulization.html

http://blog.cnbang.net/tech/3080/

©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容