基于RXSwift 下 页面跳转进行解耦 非ViewController类实现页面跳转

19 篇文章 0 订阅
3 篇文章 0 订阅

在日常开发过程中我们为了使这个项目,页面精简 美观易懂,往往采用组件式开发,会对一个复杂的完整页面分割成一个个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类实现页面跳转优化 与 不能跳转的优化

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值