iOS Can't dismiss view controller
我的应用程序有问题。 场景很简单,成功创建帐户后,我将不会关闭当前页面或导航至登录页面。 我的故事板如下所示:
成功创建帐户后,我会弹出一个对话框,其中包含一些可以的信息,我们将向您发送验证电子邮件,此弹出窗口之后,我想转到页面的第二左-这是我的主应用程序页面(现称为" View Controller")。
我尝试关闭窗口,但是在那里没有任何效果,它只能关闭弹出窗口。
当我尝试重定向时,当按下后退按钮时出现问题,这将导致"注册"页面。 有一些代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Create new user and send verification email Auth.auth().createUser(withEmail: userEmail, password: userPassword) { user, error in if error == nil && user != nil { self.sendVerificationMail(); self.displayAlertMessage(alertTitle:"Success", alertMessage:"Your account created successfully. We send you a verification email."); // Redirect to View Controller } else { self.displayAlertMessage(alertTitle:"Unhandled error", alertMessage:"Undefined error #SignUpViewController_0002"); } } ... func displayAlertMessage(alertTitle: String, alertMessage:String, alertRedirection:String =""){ let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert); let okAction = UIAlertAction(title:"Ok", style: UIAlertAction.Style.default, handler: nil); alert.addAction(okAction); self.present(alert, animated:true, completion:nil); } |
如果我添加此:
self.view.window!.rootViewController?.dismiss(动画:false,完成:nil)
警报后,仅关闭警报,警报前不执行任何操作(与关闭相同)。
要关闭并弹出主视图,可以使用警报按钮动作处理程序。
1 2 3 4 5 6 | alert.addAction(UIAlertAction(title:"OK", style: UIAlertAction.Style.default, handler: { (action) in self.dismiss(animated: true, completion: { self.navigationController?.popToRootViewController(animated: true) }) })) |
或者,您可以使用下面的几行导航到特定的视图控制器。
1 2 3 4 5 6 | for viewController in self.navigationController!.viewControllers { if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) { self.navigationController?.popToViewController(viewController, animated: true) break } } |
要在显示警报弹出窗口时盲目导航至主视图,可以在显示警报时使用完成处理程序。
1 2 3 4 5 | self.present(alert, animated: true, completion: { DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { self.navigationController?.popToRootViewController(animated: true) } }) |
查看控制器类:
1 2 3 4 5 6 7 8 9 10 11 12 | import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { self.performSegue(withIdentifier:"notLoggedView", sender: self); } } |
班级ID设置为"测试":
"推":
1 2 3 4 5 | ... let storyboard = UIStoryboard(name:"Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier:"test") as ViewController navigationController?.pushViewController(vc, animated: true) ... |
和可悲的错误:
'UIViewController' is not convertible to 'ViewController'
我在哪里弄错了?
好吧,您正在使用导航控制器,例如,要"呈现"一个新的视图控制器,您需要对其进行推送。
1 2 3 | let storyboard = UIStoryboard(name:"Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("IDOFYOURVIEW") as CLASS_NAME_OFYOUR_VIEWCONTROLLER navigationController?.pushViewController(vc, animated: true) |
使用最后的代码,您可以"呈现"(推动)新的视图控制器
现在,如果您想在按下后退按钮时执行其他操作,请尝试使用此行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | override func viewDidLoad { super.viewDidLoad() self.navigationItem.hidesBackButton = true let newBackButton = UIBarButtonItem(title:"Back", style: UIBarButtonItemStyle.Bordered, target: self, action:"back:") self.navigationItem.leftBarButtonItem = newBackButton } func back(sender: UIBarButtonItem) { //in this part you can move to other view controller, examples // Go back specific view in your navigation controller for controller in self.navigationController!.viewControllers as Array { if controller.isKind(of: NAMECLASS_OFYOUR_VIEWCONTROLLER.self) { _ = self.navigationController!.popToViewController(controller, animated: true) break } } // Perform your custom actions // ... // Go back to the previous ViewController self.navigationController?.popViewControllerAnimated(true) } |
问候