基於RXSwift 下 頁面跳轉進行解耦 非ViewController類實現頁面跳轉

在日常開發過程中我們爲了使這個項目,頁面精簡 美觀易懂,往往採用組件式開發,會對一個複雜的完整頁面分割成一個個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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章