Navigation Stack

Below is the code for a tutorial in my app. At the end of the tutorial, there is a button called 'Continue to App'. When this button is pressed, I would like to take the user to my 'storyboardview' however at the moment the button currently performs no action.

Any help would be very much welcomed.

Many thanks...

import Foundation

import SwiftUI

import UIKit



struct CameraHelp: View {

    @State private var cameraIndex = 0

    private let cameras: [Camera] = Camera.sampleCameras

    private let dotAppearance = UIPageControl.appearance()

    var body: some View {

        TabView(selection: $cameraIndex) {

            ForEach(cameras) { camera in

                VStack {

                    Spacer()

                    CameraView(camera: camera)

                    Spacer()

                    //

                    HStack {

                        //Spacer()

                        if camera == cameras.last {

                            //Button("Continue to App", action: goToZero)

                            Button("Continue to App", action: GoToApp)

                                .buttonStyle(.bordered)

                        }

                        

                    //    else {

                    //        Button("Next", action: incrementCamera)

                    //            .buttonStyle(.borderedProminent)

                    //    }

                        

                    }

                    

                    

                    Spacer()

                    Spacer()

                    Spacer()

                    Spacer()

                    Spacer()

                    Spacer()

                    //Spacer()

                    

                }

                .tag(camera.tag)

            }

        }

        .animation(.easeInOut, value: cameraIndex)// 2

        .indexViewStyle(.page(backgroundDisplayMode: .interactive))

        .tabViewStyle(PageTabViewStyle())

        .onAppear {

            dotAppearance.currentPageIndicatorTintColor = .black

            dotAppearance.pageIndicatorTintColor = .gray

        }

    }





//    func incrementCamera() {

//        cameraIndex += 1

//    }



    func GoToApp() {

        //Tutorial()

        var body: some View {

            abc()

        }

    }



    





    struct ContentView_Previews: PreviewProvider {

        static var previews: some View {

            Tutorial()

        }

    }

}





// Action on 'Camera' tab:

struct abc: View {

    var body: some View {

        storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)

    }

}

The transition to the next view does not occur through the action. But through a Navigation.

You should use a NavigationLink:

Change as follows:

struct CameraHelp: View {

    @State private var cameraIndex = 0
    private let cameras: [Camera] = Camera.sampleCameras
    private let dotAppearance = UIPageControl.appearance()

    var body: some View {
        NavigationView { // <<-- ADD THIS
           TabView(selection: $cameraIndex) {

            // more code

                    HStack {
                        if camera == cameras.last {
                           Button(action: {
                               print("Go to storyboardView") // Just to see something
                           }, label: {
                                 NavigationLink(destination: abc()) {    // That causes the transition to the storyboardview
                                    Text("Continue to App")
                            }
                         })
                 .buttonStyle(.bordered)

            // more code

You don't need GoToApp ; and in anycase, it should be declared as a View struct.

Hi,

Many thanks for your help. The program now successfully opens up the storyboard when the continue to app button is pressed - just as I wanted it to. However, unfortunately, when the continue to app button is pressed, the storyboard is opened within the navigation stack. Therefore, I end up with a view inside a view. What I need is for the app to close the navigation stack and open up my storyboard.

Any help would be greatly appreciated.

Many thanks.

My GUI is shown in the code below:

The navigation stack (tutorial) is in ContentView5. When the continue to app is pressed at the end of the tutorial, I would like to take my user to the camera tab of my GUI.

import SwiftUI

import UIKit

import MapKit



struct ContentView100: View {

    

    @State var selected = 2  // The app will open on this page.

    

    var body: some View {

        

        TabView(selection: $selected) {

            

            

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Button(action: {}) {

                //HapticsManager.shared.vibrate(for: .success)

                ContentView5().edgesIgnoringSafeArea(.all) // Naviagtion Stacks (Help page)

                

            }

            

            .tabItem {

                Image(systemName: "questionmark.circle.fill")

                Text("Help")

            }.tag(0)  //On menu bar

            

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Button(action: {}) {

               //HapticsManager.shared.vibrate(for: .success)

                storyboardview2().edgesIgnoringSafeArea(.all) // ViewController2 (Photos page)

                

            }

            

            .tabItem {

                Image(systemName: "photo")

                Text("Photos")

            }.tag(1)  //On menu bar

            

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Button(action: {}) {

                //HapticsManager.shared.vibrate(for: .success)

                storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)

                

            }

            

            .tabItem {

                Image(systemName: "camera")

                Text("Camera")

            }.tag(2)  //On menu bar

            

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Button(action: {}) {

               // HapticsManager.shared.vibrate(for: .success)

                storyboardview3().edgesIgnoringSafeArea(.all) // ViewController3 = OCR - does not belog here (History page)

                

            }

            

            .tabItem {

                Image(systemName: "clock")

                Text("History")

            }.tag(3)  //On menu bar

            

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Button(action: {}) {

              //  HapticsManager.shared.vibrate(for: .success)

                Tutorial().edgesIgnoringSafeArea(.all) // Tutorial - does not belong here (Settings page)

                

            }

            

            .tabItem {

                Image(systemName: "gearshape")

                Text("Settings")

            }.tag(4)  //On menu bar

            



        }

        

    }

    

    

    

    

    

    

    struct Company: Identifiable, Hashable {

        var id = UUID()

        let ticker: String

    }

    

    

    

    

    

    

    

    struct storyboardview: UIViewControllerRepresentable{

        func makeUIViewController(context content: Context) -> UIViewController {

            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

            let controller = storyboard.instantiateViewController(identifier: "takePhoto")

            return controller

        }

        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

            

        }

    }

    

    

    

    

    

    struct storyboardview2: UIViewControllerRepresentable{

        func makeUIViewController(context content: Context) -> UIViewController {

            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) //Working

            let controller = storyboard.instantiateViewController(identifier: "selectPhoto") //Working

            return controller //Working

        }

        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

            

        }

    }

}













class Success2: UIViewController {



    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    

    @objc public func successfullyBookedFlight() {

        HapticsManager.shared.vibrate(for: .success)

    }

    

}




We miss a lot of code to be able to test.

.

When the continue to app is pressed at the end of the tutorial, I would like to take my user to the camera tab

Did you try to change the value of selected to the camera tab in the action of "continue to app" ?

In addition, when you paste code, avoid all the extra blank lines. It is totally impossible to read. Use paste and Match Style to paste code.

import SwiftUI
import UIKit
import MapKit

struct ContentView100: View {

    @State var selected = 2  // The app will open on this page.

    var body: some View {
        TabView(selection: $selected) {

            Button(action: {}) {
                //HapticsManager.shared.vibrate(for: .success)
                ContentView5().edgesIgnoringSafeArea(.all) // Naviagtion Stacks (Help page)
            }
            .tabItem {
                Image(systemName: "questionmark.circle.fill")
                Text("Help")
            }.tag(0)  //On menu bar

            Button(action: {}) {
               //HapticsManager.shared.vibrate(for: .success)

                storyboardview2().edgesIgnoringSafeArea(.all) // ViewController2 (Photos page)
            }
            .tabItem {
                Image(systemName: "photo")
                Text("Photos")
            }.tag(1)  //On menu bar

            Button(action: {}) {
                //HapticsManager.shared.vibrate(for: .success)
                storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
            }
            .tabItem {
                Image(systemName: "camera")
                Text("Camera")
            }.tag(2)  //On menu bar

            Button(action: {}) {
               // HapticsManager.shared.vibrate(for: .success)
                storyboardview3().edgesIgnoringSafeArea(.all) // ViewController3 = OCR - does not belog here (History page)
            }
            .tabItem {
                Image(systemName: "clock")
                Text("History")
            }.tag(3)  //On menu bar

            Button(action: {}) {
              //  HapticsManager.shared.vibrate(for: .success)
                Tutorial().edgesIgnoringSafeArea(.all) // Tutorial - does not belong here (Settings page)
            }
            .tabItem {
                Image(systemName: "gearshape")
                Text("Settings")
            }.tag(4)  //On menu bar

        }

    }

    struct Company: Identifiable, Hashable {

        var id = UUID()
        let ticker: String

    }

    struct storyboardview: UIViewControllerRepresentable{

        func makeUIViewController(context content: Context) -> UIViewController {

            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let controller = storyboard.instantiateViewController(identifier: "takePhoto")

            return controller

        }

        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

        }

    }

    struct storyboardview2: UIViewControllerRepresentable{

        func makeUIViewController(context content: Context) -> UIViewController {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) //Working
            let controller = storyboard.instantiateViewController(identifier: "selectPhoto") //Working

            return controller //Working

        }

        func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

        }

    }

}

class Success2: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @objc public func successfullyBookedFlight() {

        HapticsManager.shared.vibrate(for: .success)

    }

}

Below is a screenshot of the issue I am having when the continue to app button is pressed. You can see that one ViewController has opened onto of the other.

]

Where is the Continue to App button in the last code you posted ?

You can see that one ViewController has opened onto of the other.
  • one ViewController : WHICH ?
  • onto of the other: WHICH other ?

As we don't understand to which code the posted picture corresponds, it is just impossible to say…

So basically the tutorial with the 'Continue to App' button on tab 0 of my GUI. When the 'Continue To App' button is pressed on tab 0, I want to take the user to tab 3 of my GUI.

The code for tab 0 is shown below:

import Foundation
import SwiftUI
import UIKit
import MapKit
struct Tutorial: View {
    @State private var pageIndex = 0
    private let pages: [Page] = Page.samplePages
    private let dotAppearance = UIPageControl.appearance()
    var body: some View {
        TabView(selection: $pageIndex) {
            ForEach(pages) { page in
                VStack {
                    Spacer()
                    PageView(page: page)
                    Spacer()
                    HStack {
                        if page == pages.last { // When you get to the last page, show the button 'Continue to App'.
                            Button(action: {
                                 print("Go to storyboardView") // Just to see something
                            }, label: {
                                NavigationLink(destination: goToCameraTab()) {    // That causes the transition to the storyboardview
                                    Text("Continue to App")
                                   }
                                }).buttonStyle(.bordered)
                            }
                    //    else {
                      //      Button("Next", action: incrementPage) // Show the next button on each page.
                        //        .buttonStyle(.borderedProminent)
                    //    }
                        if pageIndex == 2 { // While on page 2,
                            Button(action: {
                                 print("Go to storyboardView") // Just to see something
                            }, label: {
                                NavigationLink(destination: photosHelp()) {    // That causes the transition to the storyboardview
                                    Text("More Info")
                                   }
                                }).buttonStyle(.bordered)
                            }
                        else if pageIndex == 3 { // While on page 3,
                            Button(action: {
                                 print("Go to storyboardView") // Just to see something
                            }, label: {
                                NavigationLink(destination: cameraHelp()) {    // That causes the transition to the storyboardview
                                    Text("More Info")
                                   }
                                }).buttonStyle(.bordered)
                            }
                        else if pageIndex == 5 { // While on page 4,
                            Button(action: {
                                 print("Go to storyboardView") // Just to see something
                            }, label: {
                                NavigationLink(destination: historyHelp()) {    // That causes the transition to the storyboardview
                                    Text("More Info")
                                   }
                                }).buttonStyle(.bordered)
                            }
                        else if pageIndex == 6 { // While on page 5,
                            Button(action: {
                                 print("Go to storyboardView") // Just to see something
                            }, label: {
                                NavigationLink(destination: settingsHelp()) {    // That causes the transition to the storyboardview
                                    Text("More Info")
                                   }
                                }).buttonStyle(.bordered)
                            }
                    }
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                        Spacer()
                    }
                    .tag(page.tag)
                }
            }
            .animation(.easeInOut, value: pageIndex)// 2
            .indexViewStyle(.page(backgroundDisplayMode: .interactive))
            .tabViewStyle(PageTabViewStyle())
            .onAppear {
                dotAppearance.currentPageIndicatorTintColor = .black
                dotAppearance.pageIndicatorTintColor = .gray
            }
        }
/////////////////////////////////
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            Tutorial()
            //ContentView()
        }
    }
}
// Action on More Info 'Photos':
struct photosHelp: View {
    var body: some View {
        //storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
        PhotoHelp()
    }
}
// Action on More Info 'Camera':
struct cameraHelp: View {
    var body: some View {
        //storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
        CameraHelp()
    }
}
// Action on More Info 'History':
struct historyHelp: View {
    var body: some View {
        //storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
        HistoryHelp()
    }
}
// Action on More Info 'Settings':
struct settingsHelp: View {
    var body: some View {
        //storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
        SettingHelp()
    }
}
// Action on 'Camera' tab:
struct goToCameraTab: View {
    var body: some View {
        //storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
        ContentView6()
    }
}
// Action on 'Photo' tab:
//struct goToPhotoTab: View {
//    var body: some View {
//        storyboardview2().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
//    }
//}

Here are some screenshot images which may help.

My GUI is shown in the image below:

And the code:

import SwiftUI
import UIKit
public struct ContentView6: View {
    @State public var tabSelection = 2  // The app will open on this page.
    public var body: some View {
        TabView(selection: $tabSelection) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            helpView()
                .tabItem {
                    Image(systemName: "questionmark.circle.fill") // SF symbol
                    Text("Help") // Text beneath symbol
                }
                .tag(0)  // 1st item on menu bar
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            selectPhotoView()
                .tabItem {
                    Image(systemName: "photo") // SF symbol
                    Text("Photos") // Text beneath symbol
                }
                .tag(1)  // 2nd item on menu bar
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            takePictureView()
                .tabItem {
                    Image(systemName: "camera") // SF symbol
                    Text("Camera") // Text beneath symbol
                }
                .tag(2)  // 3rd item on menu bar
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            historyView()
                .tabItem {
                    Image(systemName: "clock") // SF symbol
                    Text("History") // Text beneath symbol
                }
                .tag(3)  // 4th item on menu bar
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            settingsView()
                .tabItem {
                    Image(systemName: "gearshape") // SF symbol
                    Text("Settings") // Text beneath symbol
                }
                .tag(4)  // 5th item on menu bar
        }
    }
}
// Action on 'Help' tab:
struct helpView: View {
    var body: some View {
        ContentView5().edgesIgnoringSafeArea(.all) // Naviagtion Stacks (Help page)
    }
}
// Action on 'Photos' tab:
struct selectPhotoView: View {
    var body: some View {
        storyboardview2().edgesIgnoringSafeArea(.all) // ViewController2 (Photos page)
    }
}
// Action on 'Camera' tab:
struct takePictureView: View {
    var body: some View {
        storyboardview().edgesIgnoringSafeArea(.all) // ViewController (Camera page)
    }
}
// Action on 'History' tab:
struct historyView: View {
    var body: some View {
        storyboardview3().edgesIgnoringSafeArea(.all) // ViewController3 = OCR - does not belog here (History page)
    }
}
// Action on 'Settings' tab:
struct settingsView: View {
    var body: some View {
        //Tutorial().edgesIgnoringSafeArea(.all) // Tutorial - does not belong here (Settings page)
        SettingsView()
    }
}
struct ContentView6_Previews: PreviewProvider {
    static var previews: some View {
        ContentView6()
    }
}

And the tutorial with the more info button is called on tab 0, this is under ContentView5. An image of this is shown below:

Navigation Stack
 
 
Q