我有以下函数,可以识别嵌入在选项卡栏 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/