在日常开发过程中我们为了使这个项目,页面精简 美观易懂,往往采用组件式开发,会对一个复杂的完整页面分割成一个个UI view ,UItableviewcell 等控件类,或者我们会在某些网络请求回调 和异步操作跳转页面。如何在这些类内进行页面跳转呢。
class NavigatorService : BaseService{
static let publishSubject = PublishSubject<Publishable>()
static let disposeBag = DisposeBag.init()
init() {
}
static func publish(_ element: Publishable) {
publishSubject.onNext(element)
}
//MARK: - 导航到视图
class func navigateToPage(_ viewCon:UIViewController, animated:Bool) -> () {
let rootCon = UIApplication.shared.keyWindow?.rootViewController
if let tabCon = rootCon as? UITabBarController {
if let selectedCon = tabCon.selectedViewController as? UINavigationController{
selectedCon.pushViewController(viewCon, animated: animated)
}else{
debugPrint("不能跳转时的\(viewCon)")
}
}else if let navigationCon = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController{
navigationCon.navigationController?.pushViewController(viewCon, animated: animated)
}else {
let newNavigationCon = UINavigationController.init(rootViewController: rootCon!)
newNavigationCon.navigationController?.pushViewController(viewCon, animated: animated)
}
}
class func navigateToPage2(_ viewCon:UIViewController, animated:Bool) -> () {
navigationController?.pushViewController(viewCon, animated: animated)
}
class func pop(_ animated:Bool) {
let rootCon = UIApplication.shared.keyWindow?.rootViewController
if let tabCon = rootCon as? UITabBarController {
if let selectedCon = tabCon.selectedViewController as? UINavigationController{
selectedCon.popViewController(animated: animated)
}else{
debugPrint(" 当前 ViewController 不是 NavigationController ,不能pop")
}
}else if let navigationCon = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController{
navigationCon.popViewController(animated: animated)
}else {
debugPrint("当前 ViewController 不是 NavigationController ,不能pop")
}
}
class func pop2(_ animated:Bool) {
navigationController?.popViewController(animated: animated)
}
// 模态视图
class func modelToPage(_ viewCon:UIViewController, animated:Bool, completion:@escaping () -> ()) -> () {
var rootCon = UIApplication.shared.keyWindow?.rootViewController
while ((rootCon?.presentedViewController) != nil)
{
rootCon = rootCon?.presentedViewController;
}
rootCon?.present(viewCon, animated: animated, completion: {
completion()
})
}
class func dismiss(_ animated:Bool, completion:@escaping () -> ()) {
let rootCon = UIApplication.shared.keyWindow?.rootViewController
if let modelCon = rootCon {
if modelCon.presentationController != nil {
modelCon.dismiss(animated: animated, completion: {
completion()
})
}else{
debugPrint("当前不能 dismiss")
}
}
}
// 模态视图
class func navigateView() -> UIView {
var rootCon = UIApplication.shared.keyWindow?.rootViewController
while ((rootCon?.presentedViewController) != nil)
{
rootCon = rootCon?.presentedViewController;
}
return (rootCon?.view)!
}
}
//使用
let vc = XXXXView()
NavigatorService.modelToPage(vc, animated: true, completion:{})
NavigatorService.navigateToPage(vc, animated: true)
NavigatorService.pop(true)
NavigatorService.dismiss(true, completion: {})
这里用到了RXSwift
基于RXSwift 下 页面跳转进行解耦 非ViewController类实现页面跳转优化 与 不能跳转的优化