What are the methods for jumping a page?

What are the methods for jumping a page?

That depends on what you mean by "jumping a page".
I don't think that's a recognized developer term, can you clarify your question?

Do you mean in navigation, go directly to VC after the next one ?

If so, there is a simple way.

I assume you have given storyboard ID to the VCs in the navigation stack as VC1, VC2, VC3…

If you want to go from VC1 to VC3 skipping VC2, several options:

  • hardwire a segue from VC1 to VC2…
  • do it programmatically.
  • with a button in VC1, with IBAction:
    @IBAction func skipNextVC(_ sender: UIButton) {
        if let vcNextPlus2 = storyboard?.instantiateViewController(withIdentifier: "VC3") {
            navigationController?.pushViewController(vcNextPlus2, animated: true)
        }
    }

Unfortunately, the following does not work, as next VC are not yet instantiated:

//        if let navController = self.navigationController {
//            let allVCs = navController.viewControllers
//            print("skip", navController, allVCs)
//            for (indexVC, vc) in allVCs.enumerated() {
//                if vc == self {
//                    let indexPlus2 = indexVC + 2
//                    if indexPlus2 < allVCs.count {
//                        let vcNextPlus2 = allVCs[indexPlus2]
//                        navController.pushViewController(vcNextPlus2, animated: true)
//                    }
//                }
//            }
//        }

But you can do something close:

  • create an array of VC ids
let vcIDs = ["VC1", "VC2", "VC3", "VC4", "VC5"]

Set restorationID equal to storyboard ID, by checking "Use Storyboard ID" in Identity inspector

    @IBAction func skipNextVC(_ sender: UIButton) {
       
        let nToSkip = 2  // skip n
        // Set restorationID equal to storyboard ID, by checking "Use Storyboard ID" in Identity inspector
        if let id = self.restorationIdentifier, let originatingIndex = vcIDs.firstIndex(of: id) {
            let indexPlus = originatingIndex + 1 + nToSkip
            if indexPlus < vcIDs.count {
                if let vcNextPlus = storyboard?.instantiateViewController(withIdentifier: vcIDs[indexPlus]) {
                    navigationController?.pushViewController(vcNextPlus, animated: true)
                }
            }
        }
}

In SwiftUI, the simplest is to select a destination in navigationLink

  • declare var to know the case
     var next : Int // 1 or 2 depending on case
     @State var fire = false     // we fire link
  • With viewBuilder, select the appropriate view
    @ViewBuilder func destination(nextView: Int) -> some View {
        switch nextView {
            case 1: View1()
            case 2: View2()
            default: EmptyView()
        }
    }
  • Use it In link
                   Button(action: {
                        fire = true
                    }) {
                        NavigationLink(destination: destination(nextView: next ?? 3), isActive:  $fire) { // 3  do nothing
                            Text("Continue")
                        }
                    }
What are the methods for jumping a page?
 
 
Q