Swift URLNavigator 回调传值
1. NavigationDetailVC
import UIKit
import URLNavigator
// NavigationHomeVC ---push---> NavigationDetailVC
// NavigationDetailVC ---pop(回调传参)---> NavigationHomeVC
class NavigationDetailVC: BaseViewController {
// 回调传参 1
var dismissCallBack: ((String) -> ())?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "URLNavigator 详情页"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 回调传参 2
dismissCallBack?("回调传参测试")
pop()
}
}
2. NavigationHomeVC
import UIKit
import URLNavigator
class NavigationHomeVC: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "URLNavigator 首页"
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 回调传参 3
var context: [String: Any] = [:]
context["dismissCallBack"] = { (test: String?) in
// 这里就是回调传过来的值了
printLog(test)
}
// 回调传参 4
AppRouter.push("FastKit://test", context: context)
}
}
3. AppRouterManger
import Foundation
import URLNavigator
public let AppRouter = AppRouterManger.default
enum AppRouterManger {
static fileprivate let `default` = Navigator()
static func initialize(navigator: NavigatorType) {
navigator.register("FastKit://test") { url, values, context in
let vc = NavigationDetailVC()
if let dict = context as? [AnyHashable: Any] {
vc.dismissCallBack = dict["dismissCallBack"] as? ((String?) -> Void)
}
return vc
}
}
}
4. AppDelegate
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册路由
AppRouterManger.initialize(navigator: AppRouter)
return true
}
}
6. 打印传递过来的值
方法:touchesBegan(_:with:)
行号:29
打印信息:Optional("回调传参测试")
来不及了,快上车