[英]how to perform segue from root view controller to the last view controller in the navigation stack in swift?
I have 3 View controllers that embedded in the navigation controller stack. 我有3个嵌入在导航控制器堆栈中的View控制器。 RedVC, BlueVC and YellowVC. RedVC,BlueVC和YellowVC。 In the each view controllers there are button that will trigger the segue 在每个视图控制器中,都有一个按钮将触发segue
from RedVC, I want to navigate either to BlueVC or to YellowVC. 从RedVC,我想导航到BlueVC或YellowVC。 All the segue from RedVC to BlueVC and to YellowVC are connected using push show segue, not present modally. 从RedVC到BlueVC到YellowVC的所有segue都使用推送显示segue连接,没有模态显示。
There is no problem if the route is like this : RedVC -> BlueVC -> YellowVC 如果路由是这样,则没有问题:RedVC-> BlueVC-> YellowVC
but if I push show segue from redVC directly to YellowVC, the back button in the YellowVC will direct me back to RedVC 但是如果我将show segue从redVC直接推到YellowVC,YellowVC中的“后退”按钮会将我定向回到RedVC
I want, if I segue from RedVC to YellowVC, I want the back button in the YellowVC will direct me to BlueVC, not RedVC. 我想要,如果我从RedVC切换到YellowVC,我希望YellowVC中的“后退”按钮会将我定向到BlueVC,而不是RedVC。
how to achieve this? 如何做到这一点?
If you are moving from RedVC to YellowVC so you could not come back to BlueVC after tapping back of yellowVC. 如果您要从RedVC迁移到YellowVC,则在点击yellowVC后就无法回到BlueVC。
But this can be achieve in a way like: 但这可以通过以下方式实现:
Keep a flag variable called : shouldShowYellowDirectly
in BlueVC and set its value true
and from RedVC in prepareForSegueMethod. 在BlueVC中保留一个名为: shouldShowYellowDirectly
的标志变量,并在prepareForSegueMethod中从RedVC设置其值为true
。
In BlueVC do this: 在BlueVC中执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
if shouldShowYellowDirectly{
performSegue(withIdentifier: "showYellow", sender: self)
}
}
It will show BlueVC for fraction of second. 它将显示BlueVC秒。
If you are to move from RedVC to YellowVC directly (skipping BlueVC ), it will rule out the possibility of BlueVC getting initialised. 如果要直接从RedVC迁移到YellowVC (跳过BlueVC ),将排除BlueVC初始化的可能性。 Thereby, when you'll perform back navigation from YellowVC , you have to forcefully initialise BlueVC for achieving what you want. 因此,当您从YellowVC执行向后导航时,必须强制初始化BlueVC以实现所需的功能。
Though this is not advisable as it won't follow the default VC life cycle. 尽管不建议这样做,因为它不会遵循默认的VC生命周期。 But if you are to do so, you need to explicitly take care of the behaviour. 但是,如果要这样做,则需要明确照顾其行为。 As an example - persisting data. 例如-持久化数据。
One solution is to add another non-animated segue between red and blue. 一种解决方案是在红色和蓝色之间添加另一种非动画序列。
Uncheck the "Animates" checkbox in the property inspector. 取消选中属性检查器中的“动画”复选框。
To go to yellow, perform this segue first and pass a true
flag that indicates whether we are going to yellow to blue. 要变黄,请先执行此segue并传递一个true
标志,该标志指示我们是否要从黄变蓝。 In blue, handle this flag. 蓝色,处理此标志。 If the flag is true, perform the animated segue to yellow. 如果该标志为真,则将动画设置为黄色。 If we are just going to blue, the animated segue to blue will be performed and the flag will be false
. 如果我们只是要变蓝,则将执行动画化为蓝色的segue,并且标志将为false
。
Another solution is to do this programmatically without segues. 另一个解决方案是以编程方式做到这一点而不会引起争执。 Give your blue and yellow VCs some identifiers: 为您的蓝色和黄色VC提供一些标识符:
To go to yellow, instantiate your blue and yellow VCs: 要变为黄色,请实例化您的蓝色和黄色VC:
let storyboard = UIStoryboard.main!
let blue = storyboard.instantiateViewController(withIdentifier: "Blue")
let yellow = storyboard.instantiateViewController(withIdentifier: "Yellow")
And then push blue unanimated, and then yellow animated: 然后将蓝色动画化,然后将黄色动画化:
navigationController?.pushViewController(blue, animated: true)
navigationController?.pushViewController(yellow, animated: false)
Yes you can insert blueVC in prepareForSegue method. 是的,您可以在prepareForSegue方法中插入blueVC。 You can follow below code 您可以按照以下代码
override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) {
super.performSegueWithIdentifier(identifier, sender: sender);
if identifier == "xyz" {
var navArray : [AnyObject]! = self.navigationController!.viewControllers
navArray.insert(blueVC, atIndex: navStackArray.count - 2)
self.navigationController!.setViewControllers(navArray, animated:false)
}
}
In above, blueVC you need to crate that object and put there. 在上面的blueVC中,您需要包装该对象并将其放在那里。 Hope this help! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.