Search code examples
swiftswiftuiuialertviewswiftui-navigationlinkswiftui-button

How to transition to a new view in swiftui


My goal is to have the user click a button that then gives them a choice of yes or cancel, this works fine. Is yes is selected it should move to the Camera View. I am getting the error: Result of 'NavigationLink<Label, Destination>' initializer is unused.

struct ContentView: View {

@State private var showAlert = false

 var body: some View {
        NavigationStack
        {
                VStack {
                        
                    Button{
                        showAlert = true
                    } label: {
                        Text("+")
                    }
                        .frame(width: 40, height: 40)
                        .symbolVariant(.fill)
                        .background(.red)
                        .cornerRadius(15)
                        .foregroundColor(.white)
                        .padding(.trailing,300)
                    Spacer() 
            }
            .alert("Create Event Here?", isPresented: $showAlert) {
                Button("Yes"){NavigationLink("addCameraView", destination: CameraView())//*****gets the error
                }
                Button("Cancel", role: .cancel) { }
                }
        }
struct CameraView: View{
    
    @State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
    @State private var selectedImage: UIImage?
    @State private var imagePickerDisplay = false

    var body: some View {
        
        NavigationView {
            VStack {
                if selectedImage != nil {
                    Image(uiImage: selectedImage!)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .clipShape(Circle())
                        .frame(width: 300, height: 300)
                } else {
                    Image(systemName: "snow")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .clipShape(Circle())
                        .frame(width: 300, height: 300)
                }
                Button("Camera") {
                    self.sourceType = .camera
                    self.imagePickerDisplay.toggle()
                }.padding()
            }
            .navigationBarTitle("Take a Photo of the Event")
            .sheet(isPresented: self.$imagePickerDisplay) {
                ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
            }
        }
    }
}
}

Solution

  • To navigate with a button, we need to utilize a variable as our trigger. Wrapping the button in a NavigationLink and updating the associated variable to the appropriate value will trigger the navigation.

    Below you will find the updated ContentView. CameraView remains unchanged.

    import SwiftUI
    
    struct ContentView: View {
        
        @State private var showAlert = false
        @State var selection: Int? = nil
        
        var body: some View {
            NavigationStack
            {
                
                VStack {
                    
                    Button{
                        showAlert = true
                    } label: {
                        Text("+")
                    }
                    .frame(width: 40, height: 40)
                    .symbolVariant(.fill)
                    .background(.red)
                    .cornerRadius(15)
                    .foregroundColor(.white)
                    .padding(.trailing,300)
                    Spacer()
                }
                .alert("Create Event Here?", isPresented: $showAlert) {
                    NavigationLink(destination: CameraView(), tag: 1, selection: $selection) {
                        Button("Yes"){
                            selection = 1
                        }
                    }
                    Button("Cancel", role: .cancel) {
                        selection = nil
                    }
                    
                }
            }
        }
    }