如果导航堆栈中不存在,则推送 viewcontroller
Push a viewcontroller if it is not present in the navigation stack
我想检查导航堆栈中是否存在 viewcontroller。如果它存在,我需要弹出它,否则我需要将它推送到导航堆栈。我尝试了以下代码。如果它不存在,控制权将转移到 else 块,但我无法导航到屏幕。请帮助我
for aViewController in viewControllers! {
if aViewController is TabProfileViewController {
self.navigationController?.popToViewController(aViewController, animated: true)
}
else {
let lvc = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(lvc!, animated: true)
}
}
你在每个循环中检查它,所以如果第一个条件一次为真,它可能在下一次迭代中变为假,所以它会弹出和推送。试试下面的代码:
if let viewController = viewControllers?.first(where: { [=10=] is TabProfileViewController }) {
navigationController?.popToViewController(viewController, animated: true)
} else {
let lvc = storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
navigationController?.pushViewController(lvc!, animated: true)
}
希望这段代码对你有用。
if navigationController != nil && !(navigationController?.topViewController is YOURCONTROLLER) {
for aViewController in (navigationController?.viewControllers)!
{
if aViewController is YOURCONTROLLER {
//Your controller found
}else{
//push using navigation
}
}
}
这是另一种方法:
if arrViewController != nil && !(arrViewController?.topViewController is TabProfileViewController) {
for aViewController in (arrViewController?.viewControllers)! {
if aViewController is Dashboard {
_ = self.navigationController?.popToViewController(aViewController, animated: true)
break
}
}
}else{
let lvc = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(lvc!, animated: true)
}
我想检查导航堆栈中是否存在 viewcontroller。如果它存在,我需要弹出它,否则我需要将它推送到导航堆栈。我尝试了以下代码。如果它不存在,控制权将转移到 else 块,但我无法导航到屏幕。请帮助我
for aViewController in viewControllers! {
if aViewController is TabProfileViewController {
self.navigationController?.popToViewController(aViewController, animated: true)
}
else {
let lvc = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(lvc!, animated: true)
}
}
你在每个循环中检查它,所以如果第一个条件一次为真,它可能在下一次迭代中变为假,所以它会弹出和推送。试试下面的代码:
if let viewController = viewControllers?.first(where: { [=10=] is TabProfileViewController }) {
navigationController?.popToViewController(viewController, animated: true)
} else {
let lvc = storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
navigationController?.pushViewController(lvc!, animated: true)
}
希望这段代码对你有用。
if navigationController != nil && !(navigationController?.topViewController is YOURCONTROLLER) {
for aViewController in (navigationController?.viewControllers)!
{
if aViewController is YOURCONTROLLER {
//Your controller found
}else{
//push using navigation
}
}
}
这是另一种方法:
if arrViewController != nil && !(arrViewController?.topViewController is TabProfileViewController) {
for aViewController in (arrViewController?.viewControllers)! {
if aViewController is Dashboard {
_ = self.navigationController?.popToViewController(aViewController, animated: true)
break
}
}
}else{
let lvc = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(lvc!, animated: true)
}