Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 关闭视图控制器不';在助手班工作_Ios_Swift - Fatal编程技术网

Ios 关闭视图控制器不';在助手班工作

Ios 关闭视图控制器不';在助手班工作,ios,swift,Ios,Swift,因此,我有一个助手类,如下所示: class Helper { static func handleTokenInvalid() { DispatchQueue.main.async { UIViewController().dismiss() } } } extension UIViewController { func dismiss() { let root = UIApplicatio

因此,我有一个助手类,如下所示:

class Helper {
   static func handleTokenInvalid() {
        DispatchQueue.main.async {
                UIViewController().dismiss()
        }
    }
}

extension UIViewController {
    func dismiss() {
        let root = UIApplication.shared.keyWindow?.rootViewController
        root?.dismiss(animated: true, completion: nil)    }
}
我想关闭所有打开并返回应用根目录的视图控制器。但是它不起作用。如果我在普通视图中执行相同的操作,则控制器是有效的。有人知道解决办法吗?谢谢大家!

编辑: 我也试过了,但它说在包装可选值时发现了nil

func dismiss() {
        self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
    }
您可以在Helper类的任何位置使用它

if let topController = UIApplication.topViewController() {
   topController.dismiss(animated: true, completion: nil)
}

你所做的一切都是为了这个

UIViewController().dismiss
正在创建新的视图控制器并将其取消。
您必须对实际显示的视图控制器实例调用dismise。

您可以更改rootViewController

UIApplication.shared.keyWindow?.rootViewController = yourController

我认为在不关闭当前应用程序窗口的根视图控制器的情况下,有更好的解决方案

将完成处理程序添加到方法中,当您从控制器内部调用它时,在闭包中声明将调用完成后,您需要关闭
self
(如果您通过
UINavigationController
推送控制器,只需弹出它)

然后在控制器中使用完成处理程序调用您的方法

class ViewController: UIViewController {

    func call() {
        Helper.handleTokenInvalid { // this is called when you call `completion` from `handleTokenInvalid`
            self.dismiss(animated: true)
            //self.navigationController?.popViewController(animated: true)
        }
    }

}

两天后,我找到了正确的方法来关闭我的控制器,但找不到任何控制器,因为我认为Xcode发现我当前的控制器为零。相反,我使用的是:

let viewController = UIStoryboard(name: "DashboardPreLogin", bundle: Bundle.main).instantiateViewController(withIdentifier: "TabBarPreLoginViewController")

let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = nil
appDel.window?.rootViewController = viewController
UIView.transition(with: appDel.window!, duration: 0.5, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {() -> Void in appDel.window?.rootViewController = viewController}, completion: nil)

这将解除view controller并替换为新控制器。

您是否看到了这一点
所有打开并返回应用根目录的view controller
请检查下面提供的答案(MCMatan),这更符合Firda@SahzaibQureshi我已经这样做了,但仍然没有返回根目录如何做?如果这样做不起作用:self.view.window?.rootViewController?.disclose(动画:true,完成:nil)您可能正在按视图控制器(通过导航控制器)这应该可以用:self.navigationController?.popToRootViewController(动画:true)我也试过,但没用。故事板有什么问题吗?它没有回答,我找到了我在这个问题中已经回答过的替代方案。谢谢你的帮助!我的应用程序中有许多控制器,因此我希望避免每个控制器都发生更改。可以在helper类中取消吗?它给我一个错误,无法将类型“yourController.type”的值分配给类型“UIViewController?”yourController。它只是一个示例词,而不是替换那里的控制器实例。
class ViewController: UIViewController {

    func call() {
        Helper.handleTokenInvalid { // this is called when you call `completion` from `handleTokenInvalid`
            self.dismiss(animated: true)
            //self.navigationController?.popViewController(animated: true)
        }
    }

}
let viewController = UIStoryboard(name: "DashboardPreLogin", bundle: Bundle.main).instantiateViewController(withIdentifier: "TabBarPreLoginViewController")

let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = nil
appDel.window?.rootViewController = viewController
UIView.transition(with: appDel.window!, duration: 0.5, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {() -> Void in appDel.window?.rootViewController = viewController}, completion: nil)