gpt4 book ai didi

iOS 无法关闭 View Controller

转载 作者:行者123 更新时间:2023-11-28 23:21:12 25 4
gpt4 key购买 nike

我的应用有问题。场景很简单,成功创建帐户后我不想关闭当前页面或导航到登录页面。我的 Storyboard如下所示:

enter image description here

成功创建帐户后,我会弹出一个包含一些信息的弹出窗口).

我试过关闭窗口,但没有效果,它只能关闭我的弹出窗口。当我尝试重定向时,按下后退按钮时出现问题,它会导致注册页面。有一些代码:

// 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(animated: false, completion: nil)

alert之后,它只关闭alert,alert之前,它什么都不做(与dismiss相同)。

最佳答案

要关闭并弹出到主视图,您可以使用警报按钮操作处理程序。

alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in

self.dismiss(animated: true, completion: {
self.navigationController?.popToRootViewController(animated: true)
})
}))

或者您可以使用下面几行导航到特定的 View Controller 。

for viewController in self.navigationController!.viewControllers {
if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) {
self.navigationController?.popToViewController(viewController, animated: true)
break
}
}

Your_Main_ViewController_Class_Name 是您需要导航到的导航 Controller 堆栈中的 View Controller 。 (即)主视图

要在显示警报弹出窗口后盲目导航到主视图,您可以在显示警报时使用完成处理程序。

self.present(alert, animated: true, completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
self.navigationController?.popToRootViewController(animated: true)
}
})

关于iOS 无法关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com