ios - 如何防止用户从滑出设置菜单中连续两次调用同一 View Controller ?

标签 ios swift uiviewcontroller uinavigationcontroller

我有以下函数,可以识别嵌入在选项卡栏 Controller 中的导航 Controller 并推送配置文件 View Controller 。此函数有效,但我想做一些检查,如果从滑出菜单调用此函数而配置文件 View Controller 是最近推送的 View Controller ,则可以防止它再次显示配置文件 View Controller 。函数如下:

private func toProfile() {
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
        let tbc = appDelegate.window?.rootViewController as? TabBarController,
        let nav =  tbc.viewControllers?[tbc.selectedIndex] as? UINavigationController else { return }
    let profileVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "profileVC")
    nav.pushViewController(profileVC, animated: true)
    delegate?.dismissSettingsVC()
}

我尝试过:

if nav.viewControllers.last == profileVC {
    print("Do nothing")
} else {
    nav.pushViewController(profileVC, animated: true)
}

但它从未说过两者相等。如何制作 if 语句来检查最后推送的 View Controller 是否是 profileVC?

最佳答案

您需要检查类型

if nav.viewControllers.last is ProfileVC {
   print("Do nothing") 
}
else { 
  nav.pushViewController(profileVC, animated: true) 
}

当前您比较相同类型的 2 个实例,并且确定它们不相等

关于ios - 如何防止用户从滑出设置菜单中连续两次调用同一 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953183/

相关文章:

ios - 报亭内存存储问题,如何获取应用程序缓存目录?

ios - 相当于 Objective-c 中的 Swift Public 方法?

ios - 快速更新循环

ios - 检索我们处于生命周期 Controller 的哪个状态

iOS 8 - 禁用 iCloud 照片共享事件

ios - 在导航 Controller 中的导航栏下方添加 View

iOS - 将累积数据传递到一长串 UIViewControllers

ios - View 不再位于 View 堆栈顶部时的事件

swift - ARKit:通过添加 anchor 将 USDZ 模型放置在平面上,但它总是在我的头顶上方

ios - 在 Swift 中为 iOS 设计填字游戏网格