gpt4 book ai didi

ios - 以编程方式返回到 Swift 中连续的第三个 ViewController

转载 作者:行者123 更新时间:2023-11-28 05:48:49 24 4
gpt4 key购买 nike

我已经看到太多关于如何返回到以前的 View Controller 的线程,这很有意义并且也很容易实现。但就我而言,只有一种并发症。考虑以下情况。

  • VC1 --Opens--> VC2: vc2 是一个显示表单的 View Controller 。正确填写后,用户将被带到另一个 VC。称它为 vC3
  • VC2 --Opens--> VC3: vc3 是一个 View Controller ,它获取刷卡信息,如果提供的信息正确,它就获取到 VC4

  • VC3 --打开--> VC4:

VC4 is the viewcontroller that loads up all user contacts and send information/data to that selected contact. SO far it is fine. now there is 2 conditions, if the data that I send is correctly sent then I have to go back to VC1 directly, and if the data is not sent then I have to go back to vc2 directly to take up information again.

那么处理这种情况最好的办法是什么。请建议。

更新:

I am using Tab bar controller, in that controller under one of that View , I am opening other VIew controller from this in series. Like from this Opening VC2 that opens VC3 and so on...

这就是我显示 ViewController 2 的方式

let myVC2 = self.storyboard?.instantiateViewController(withIdentifier: "idMyVC2") as MyVC2 
myVC2.modalPresentationStyle = .fullScreen
self.present(myVC2,animated : true)

最佳答案

将其插入 playground 中并随意使用:

import UIKit

protocol DismissDelegate: AnyObject {
func dismissVC(_ presenting: Int)
}

class VC1: UIViewController, DismissDelegate {
func dismissVC(_ presenting: Int) {
guard presenting == 1 else {
return
}
print("dismiss 2, 3, 4, 5")
}
}
class VC2: UIViewController, DismissDelegate {
weak var vc2Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 2 else {
return vc2Delegate!.dismissVC(presenting)
}
print("dismiss 3, 4, 5")
}
}
class VC3: UIViewController, DismissDelegate {
weak var vc3Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 3 else {
return vc3Delegate!.dismissVC(presenting)
}
print("dismiss 4, 5")
}
}
class VC4: UIViewController, DismissDelegate {
weak var vc4Delegate: DismissDelegate?
func dismissVC(_ presenting: Int) {
guard presenting == 4 else {
return vc4Delegate!.dismissVC(presenting)
}
print("dismiss 5")
}
}
class VC5: UIViewController {
weak var vc5Delegate: DismissDelegate?
func dismissStack(at presenting: Int) {
vc5Delegate?.dismissVC(presenting)
}
}

let vc1 = VC1()

let vc2 = VC2()
vc2.vc2Delegate = vc1

let vc3 = VC3()
vc3.vc3Delegate = vc2

let vc4 = VC4()
vc4.vc4Delegate = vc3

let vc5 = VC5()
vc5.vc5Delegate = vc4

vc5.dismissStack(at: 1) // prints: dismiss 2, 3, 4, 5
vc5.dimissStack(at: 2) // prints: dismiss 3, 4, 5

设置非常基本,每个 View Controller 都通过一个委托(delegate)链接起来。因为有多个 View Controller ,我们创建了一个协议(protocol)。该协议(protocol)只是一个采用整数参数的方法,该参数是应该执行解散的 View Controller (呈现 View Controller )。然后当你想执行一个 dismiss 时,调用委托(delegate)(不必来自第 5 个 View Controller ),指定呈现 View Controller ,委托(delegate)将沿着链向下并检查它是否是呈现 View Controller (如果是的,它会关闭它上面的所有内容)或不是(如果不是,它将退出一个 View Controller 并重复该过程)。

VC5 有一个名为 dismissStack(at:) 的方法,它允许您输入要关闭的 View Controller 的整数。因此,如果您在 2 解散,则 View Controller 3、4、5 将被解散。显然,您也可以将此方法放入其他 View Controller 中。

关于ios - 以编程方式返回到 Swift 中连续的第三个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com