Swift的路由URLNavigator

初始化配置路由表

新建NavigatorMap文件,添加如下内容

import URLNavigator

struct NavigatorMap {
    
    
    static func initialize(navigator: NavigatorType) {
        navigator.register("myapp://user/<int:id>") {url, values, context in
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "OtherViewController") as! OtherViewController
            vc.title = "dajiahao"
            vc.showText = "wohaoa"
            guard let userID = values["id"] as? Int else { return nil }
            print(userID)
            print(url)
            print(values)
            print(context!)
            vc.delegate = context as? callBackDelegate
            return vc
//            return OtherViewController()
        }
//        navigator.register("myapp://post/<title>") { ... }
//        navigator.handle("myapp://alert") { ... }
    }
}

导入头文件URLNavigator,注册url:myapp://user/<int:id>,<int:id>是一个字典数据,用于数据的传递,闭包中的url为myapp://user,values为["id": 123],context为ViewController(一般用于传输额外的字典数据,多用于代理,闭包等,当前控制器的句柄)

路由表配置完毕,我们就可以在appdelegate里面进行初始化了

在didFinishLaunchingWithOptions方法中添加如下

import UIKit
import URLNavigator

public let navigator = Navigator()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

  
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
     
        
        NavigatorMap.initialize(navigator: navigator)
        
        
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.makeKeyAndVisible()
        window.backgroundColor = .white

        
        let vc = ViewController(navigator: navigator, username: "nihao")
        window.rootViewController = UINavigationController(rootViewController: vc)
        self.window = window
        
        
        
        
        return true
    }
}

加载路由表NavigatorMap.initialize(navigator: navigator),ViewController(navigator: navigator, username: "nihao")这里有一个navigator: navigator的传输,下一个控制器的init方法中我们可以将navigator传入,然后使用navigator.push("myapp://user/123", context: self)进行跳转。我们也可以在appdelegate里面加入全局的public let navigator = Navigator()进行跳转使用,这样我们就不用每一个页面去传入navigator。

可以参考https://github.com/devxoul/URLNavigator

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,124评论 19 139
  • 原文链接:https://github.com/halfrost/Halfrost-Field/blob/mast...
    hament阅读 10,992评论 1 31
  • 前言 随着用户的需求越来越多,对App的用户体验也变的要求越来越高。为了更好的应对各种需求,开发人员从软件工程的角...
    一缕殇流化隐半边冰霜阅读 87,903评论 214 1,098
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 10,696评论 1 23
  • 道德用于律己,胜过一切律法。道德用于律人,恶过一切私刑。
    唐玉俊阅读 1,765评论 0 0

友情链接更多精彩内容