通过按下标签栏按钮关闭viewController,保持黑屏迅速5 [英] Dismissing viewController by pressing on tabbar button remain black screen swift 5

查看:66
本文介绍了通过按下标签栏按钮关闭viewController,保持黑屏迅速5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的操作表viewController .在当前的顶视图控制器上以模态显示.像这样:

I have a custom action sheet viewController. Which is presented modally on the current top view controller. Like this:

//MARK: Static Func
    static func initViewController() -> CustomActionSheetViewController {
        let customActionSheetViewController = CustomActionSheetViewController(nibName: "CustomActionSheetViewController", bundle: nil)
        return customActionSheetViewController
    }

    func presentViewController<T: UIViewController>(viewController: T) {
        DispatchQueue.main.async {
            if let topViewController = UIApplication.getTopViewController() {
                viewController.modalTransitionStyle = .crossDissolve
                viewController.modalPresentationStyle = .overCurrentContext
                topViewController.present(viewController, animated: true, completion: nil)
            }
        }
    }

// MARK: UIApplication extensions
extension UIApplication {
    class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return getTopViewController(base: nav.visibleViewController)
        } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
            return getTopViewController(base: selected)
        } else if let presented = base?.presentedViewController {
            return getTopViewController(base: presented)
        }
        return base
    }
}

我正在这样消除它:

@objc func dismissViewController() {
        DispatchQueue.main.async {
            if let topViewController = UIApplication.getTopViewController() {
               topViewController.dismiss(animated: true, completion: nil)
            }
            NotificationCenter.default.removeObserver(self)
        }
    }

工作正常.我在我的customTabbarController中添加了通知观察器,以在用户点击另一个tabbar按钮时关闭操作表,如下所示:

It's working perfectly fine. I have added the notification observer in my customTabbarController, to dismiss the action sheet if user tap on some another tabbar button like this:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
//        print("Selected view controller", viewController)
//        print("index", tabBarController.selectedIndex )
        let tabbarNotiKey = Notification.Name(rawValue: "TabbarNotiKey")
        NotificationCenter.default.post(name: tabbarNotiKey, object: nil, userInfo: nil)
    }

操作单现在显示在Home tab > Profile (by push) > Action sheet (by modal)上.因此,如果我再次点击主页"选项卡,它将关闭动作表viewController ,然后完美返回主页".但是,如果我点击其他一些标签栏按钮而不是首页,然后返回首页,它会显示黑屏.我在这里想念的是什么?任何建议都是非常可取的.

The action sheet is right now presenting on Home tab > Profile (by push) > Action sheet (by modal). So if I tap on Home tab again it will dismiss the action sheet viewController and come back to Home perfectly. But if I tap on some other tabbar button rather than home and come back home, it shows a black screen. What I am missing here? Any suggestions would be highly appreciable.

推荐答案

我猜您的代码两次调用dismissViewController().

I guess your code calls dismissViewController() twice.

  1. 按下其他标签时,它会删除操作表
  2. 然后单击主页"选项卡,它再次调用dismissViewController,现在它将删除homescreenVC
  1. When you press other tab, it removes the action sheet
  2. And then you click home tab and it calls dismissViewController again, and now it removes the homescreenVC

这篇关于通过按下标签栏按钮关闭viewController,保持黑屏迅速5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆