swift下如何配置引导页后跳转rootViewController

本文介绍在Swift应用中如何设置引导页并在完成引导后跳转至rootViewController,即一个包含多个子控制器的TabBarController。通过在AppDelegate中创建引导页ViewController,设置其闭包,在闭包中调用start()方法,更新rootViewController为包含多个子控制器的UINavigationController,从而实现从欢迎页到复杂导航结构的平滑过渡。

在写一个小demo的时候发现如果将rootViewController定义在引导页(欢迎页)上时,之前设置的导航栏和tabBar都不能正常显示于是查书查到

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //先设置欢迎页

let vc = ViewController()

        //将欢迎页设置为根视图

self.window?.rootViewController = vc

        self.window?.backgroundColor = UIColor.whiteColor()

        //调用闭包

vc.startClosure = {

        () -> Void in

            self.start()

        } 

    }

    //闭包所用的方法,用来启动之后的View,并且修改根视图

    func start(){

        let vc1 = SecondViewController()

        let vc2 = ThirdViewController()

        let vc3 = FoutViewController()

        let vc4 = FifthViewController()

        vc1.tabBarItem.title = "1"

        vc2.tabBarItem.title = "2"

        vc3.tabBarItem.title = "3"

        vc4.tabBarItem.title = "4"

        let tabber = UITabBarController()

        //设置UITabBarController

tabber.viewControllers = [vc1,vc2,vc3,vc4]

//设置UINavigationController的根视图,这里的根视图必须为tabber而不是欢迎页(vc)

        let navi = UINavigationController(rootViewController: tabber)

        self.window?.rootViewController = navi

    }

再回到欢迎页里设置闭包

var startClosure : (() -> Void)?

并在欢迎页跳转后面View的func里面调用这个闭包即可

@objc func addDeviceButtonClicked(_ sender: Any) { // sprintf("%d",appContext.userInfoList.count) // DeviceAdd.shared.reload(source: .fromAddButton) // if isConnectNetwork == true { // performSegue(withIdentifier: deviceLisetMasterToDeviceAddByQrCodeMainSegueID, sender: nil) // }else { // addButtonDidClickAlert(str: LocalizedString(key: devicelistNotConnectInternet)) // } // if showLocalDeviceOnly { // //如果是本地添加,直接跳转至批量添加 // let vc = DeviceBatchAddAutoSearchMainViewController() // DeviceAdd.shared.reload(source: .fromAddButton) // DeviceAdd.shared.model.addMethod = .byDiscover // //本地添加 // DeviceAdd.shared.model.isOnlineAddOrScanQrCode = false // vc.isFromRemoteAdd = false // vc.addMethod = .all // self.navigationController?.pushViewController(vc, animated: true) // return // } // 未登录不能云添加, 弹框提示 if (!appContext.isLogin && !showLocalDeviceOnly ) { // 登录提示 showAlertWith(title: LocalizedString(key: deviceAddNeedLoginTitle), message: LocalizedString(key: deviceAddNeedLoginDetail), defaultButtonTitle: LocalizedString(key: accountLoginVCLoginText), cancelButtonTitle: LocalizedString(key: commonLater), cancelButtonColor: UIColor.tpbRed, defaultButtonHandler: { [weak self] _ in guard let self = self else { return } guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController as? MainModuleTabBarController else { return } guard let vc = UIStoryboard(name: "AccountManager", bundle: nil).instantiateViewController(withIdentifier: "accountManagerLogin") as? AccountLoginViewController else { return } AccountLoginUIContext.shared.tabBarItemIndex = rootViewController.selectedIndex self.navigationController?.presentFullScreenViewController(vc) }) return } // 联网提示 if isConnectNetwork == false && !showLocalDeviceOnly { addButtonDidClickAlert(str: LocalizedString(key: devicelistNotConnectInternet)) return } // 本地网络权限提示 if UserDefaults.standard.needShowLocalNetworkPermissionTipWhenAddingDevice { UserDefaults.standard.needShowLocalNetworkPermissionTipWhenAddingDevice = false if let vc = UIStoryboard(name: PermissionStoryboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: PermissionLocalNetworkViewControllerIdentifier) as? PermissionLocalNetworkViewController { vc.canBack = true vc.completeClosure = { vc.navigationController?.popViewController(animated: true) DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { self.showDeviceAddMenuView() }) } navigationController?.pushViewController(vc, animated: true) return } } // 展示添加Menu showDeviceAddMenuView() }
最新发布
11-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值