ios - 导航到另一个视图后阻止显示 UIAlertViewController
问题描述
我有一种情况,当用户超过 5 分钟没有使用应用程序时,应用程序将显示一个带有会话过期消息的弹出窗口。
会话过期的代码被添加到 中,appDelegate
并且从那里弹出将显示在当前视图控制器上。
代码是
@objc func applicationDidTimeout(notification: NSNotification) {
if (window?.rootViewController?.isKind(of: UITabBarController.self))! {
for view in window?.rootViewController?.view.subviews ?? [(window?.rootViewController?.view)!] {
if view.isKind(of: MBProgressHUD.self) {
return
}
}
if window?.rootViewController?.presentedViewController != nil {
window?.rootViewController?.dismiss(animated: true, completion: {
self.showMessage(message: Message.sessionTimeout)
})
} else {
self.showMessage(message: Message.sessionTimeout)
}
}
}
fileprivate func showMessage(message: String) {
let alert = UIAlertController(title: appName, message: message, preferredStyle: .alert)
let actionOkay = UIAlertAction(title: "OK", style: .default) { (action) in
DispatchQueue.main.async {
UIView.transition(with: self.window!, duration: 0.3, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
CommonFunctions.setLoginAsRootVC()
}, completion: nil)
}
}
alert.addAction(actionOkay)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
现在,如果用户正在输入一些数据,并且在那个时候,如果用户离开应用程序的理想状态 5 分钟或更长时间,键盘将关闭并在那里显示会话到期消息。
但是由于文本字段的委托方法textFieldShouldEndEditing
有一些验证,如果验证失败,它会显示一个带有消息和确定按钮的弹出窗口。
因此,当用户在会话过期消息弹出窗口中点击确定按钮时,它会将用户重定向到登录屏幕,但由于文本字段的委托方法验证,它会在登录屏幕中显示一个弹出窗口。
验证失败消息弹出的代码是
fileprivate func showErrorMessage(message: String) {
let alert = UIAlertController(title: appName, message: message, preferredStyle: .alert)
let actionOkay = UIAlertAction(title: "OK", style: .default) { (action) in
self.txtField.becomeFirstResponder()
}
alert.addAction(actionOkay)
self.present(alert, animated: true, completion: nil)
}
如何防止弹出窗口出现在登录屏幕中?
解决方案
@objc func applicationDidTimeout(notification: NSNotification)
{
let visibleView : UIViewController = self.getVisibleViewControllerFrom(self.window?.rootViewController)!
self.showMessage(message: Message.sessionTimeout,Controller: visibleView)
}
fileprivate func showMessage(message: String , Controller : UIViewController) {
let alert = UIAlertController(title: appName, message: message, preferredStyle: .alert)
let actionOkay = UIAlertAction(title: "OK", style: .default) { (action) in
//Now apply your code here to set login view controller as rootview
// This controller is for demo
window!.rootViewController = UIStoryboard(name: "Main", bundle:
nil).instantiateViewController(withIdentifier: "loginview")
window!.makeKeyAndVisible()
}
alert.addAction(actionOkay)
Controller.present(alert, animated: true, completion: nil)
}
//MARK:- 支持从窗口获取可见视图控制器的方法
func getVisibleViewControllerFrom(_ vc: UIViewController?) -> UIViewController? {
if let nc = vc as? UINavigationController {
return self.getVisibleViewControllerFrom(nc.visibleViewController)
} else if let tc = vc as? UITabBarController {
return self.getVisibleViewControllerFrom(tc.selectedViewController)
} else {
if let pvc = vc?.presentedViewController {
return self.getVisibleViewControllerFrom(pvc)
} else {
return vc
}
}
}
试试这个代码,我已经多次使用这个代码可能对你有用。
推荐阅读
- awk - 如何在 AWK 中对给定的数据集使用匹配
- reactjs - React 和 Material-UI:使用 TypeScript 在 createStyles() 中未定义主题
- c++ - WxWidgets Hello World 无法编译:“wchar_t 必须可用”
- firebase - 带有安全规则的 Firestore where 子句
- c++ - 矢量 2D 擦除元素并保持字段为空
- algorithm - 三元搜索的递归关系
- angular - Angular 6 (jsPDF & jsBarcode) 中的 PDF 上未显示条码
- symfony - 如何更改类别实体的树视图顺序
- c++ - 将 boost (asio) 错误消息翻译成自然语言
- xamarin.forms - 将搜索词从 SearchHandler 传递到 Xamarin Forms 4 中的 ContentPage