我想在用户使用密码身份验证后转到当前打开的视图控制器。
这是我在AppDelegate中的(EnterForeground(上的代码:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let stb = UIStoryboard(name: "Main", bundle: nil)
let ps = stb.instantiateViewController(withIdentifier: "psID") as! PassC_VC
let appdelegate = UIApplication.shared.delegate as! AppDelegate
self.window?.makeKeyAndVisible()
DispatchQueue.global().async {
DispatchQueue.main.async {
ps.modalPresentationStyle = UIModalPresentationStyle.currentContext
appdelegate.window?.rootViewController = ps
print("in forground")
}
}
}
此代码成功地工作并显示此窗口,但是解散此控制器并显示当前活动视图控制器的问题,该视图控制器是在显示Passcode View Controller之前打开的。
这是我的passc_vc.swift代码:加法信息:我不使用任何Uinavigation Controller或其他东西
输入密码后,按钮单击
self.dismiss(animated: true, completion: nil)
您只能解雇控制器您 pranister 。您没有出现" PS",而是设置为window rootviewController。这意味着这是屏幕上的第一个控制器,因此解散它是没有意义的。
要回到屏幕上的控制器,您可以做以下操作之一:
选项1:查看控制器参考
当您的应用程序进入前面时,请在AppDelegate中对该控制器进行引用:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let stb = UIStoryboard(name: "Main", bundle: nil)
let ps = stb.instantiateViewController(withIdentifier: "psID") as! PassC_VC
let appdelegate = UIApplication.shared.delegate as! AppDelegate
self.window?.makeKeyAndVisible()
DispatchQueue.global().async {
DispatchQueue.main.async {
ps.modalPresentationStyle = UIModalPresentationStyle.currentContext
if let delegate = UIApplication.shared.delegate as? AppDelegate {
//Storing the reference
delegate.yourReferenceToPreviousController = delegate.window?.rootViewController
//Assigning the new controller
delegate.window?.rootViewController = ps
print("in forground")
}
}
}
}
和,在输入密码后单击"确定按钮"执行:
if let delegate = UIApplication.shared.delegate as? AppDelegate {
//You can now use your stored reference to reassign the root view controller
delegate.window?.rootViewController = delegate.yourReferenceToPreviousController
}
选项2:重新呈现先前的视图控制器
,或者您可以避免对上一个控制器进行引用,并通过执行"输入密码后的确定按钮单击"来重新确定它:
let previousController = PreviousController() //This is in code but you can initialize it via storyboard
self.present(previousController, animated: true)
选项3:正确呈现/关闭视图控制器
另一种方法是通过实际获取VisibleController并使他像这样的密码控制器来改变您显示控制器的方式:
您可以使用此扩展名来获取可见的控制器:
extension UIWindow {
func visibleViewController() -> UIViewController? {
if let rootViewController: UIViewController = self.rootViewController {
return UIWindow.getVisibleViewControllerFrom(vc: rootViewController)
}
return nil
}
private class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
if vc.isKind(of: UINavigationController.self) {
let navigationController = vc as? UINavigationController
return UIWindow.getVisibleViewControllerFrom(vc: navigationController!.visibleViewController!)
} else if vc.isKind(of: UITabBarController.self) {
let tabBarController = vc as? UITabBarController
return UIWindow.getVisibleViewControllerFrom(vc: tabBarController!.selectedViewController!)
} else {
if let presentedViewController = vc.presentedViewController {
return UIWindow.getVisibleViewControllerFrom(vc: presentedViewController)
} else {
return vc
}
}
}
}
然后在应用程序意义前绑定:
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
let stb = UIStoryboard(name: "Main", bundle: nil)
let ps = stb.instantiateViewController(withIdentifier: "psID") as! PassC_VC
let appdelegate = UIApplication.shared.delegate as! AppDelegate
self.window?.makeKeyAndVisible()
DispatchQueue.global().async {
DispatchQueue.main.async {
ps.modalPresentationStyle = UIModalPresentationStyle.currentContext
//You get the controller that is on screen
let visibleViewController = UIWindow.getVisibleViewControllerFrom(vc: window?.rootViewController!)
//Then make it present the controller you want
visibleViewController.present(ps, animated: true)
print("in forground")
}
}
}
这样,您实际上是在显示控制器,因此您可以:
self.dismiss(animated: true, completion: nil)
就像您想要的一样。
希望它有帮助。