-1

I created a page for users to register new accounts. I created a "continue" button that is meant to push the new data to firebase and simultaneously move the users to the next view which is my mapView(). Right now the signup fucntion is working, but I cant figure out how to implement the mapView()

                        Button(action:{
                            guard !email.isEmpty, !password.isEmpty else {
                                return
                            }
                            
                            viewModel.signUp(email: email, password: password)

                        } , label: {
                            Text("Continue")
                                .foregroundColor(.white)
                                .frame(width: 350, height: 35)
                                .background(Color.blue)
                                .cornerRadius(20)
                        })
        }

Ive tried adding map view inside of the function but Xcode returns a warning that says "Result of 'mapView' initializer is unused".

    Button(action:{
                            guard !email.isEmpty, !password.isEmpty else {
                                return
                            }
                            
                            viewModel.signUp(email: email, password: password)
                            mapView()
                        } , label: {
                            Text("Continue")
                                .foregroundColor(.white)
                                .frame(width: 350, height: 35)
                                .background(Color.blue)
                                .cornerRadius(20)
                        })
k.arias
  • 11
  • 2
  • Try the [Apple SwiftUI Tutorials](https://developer.apple.com/tutorials/swiftui) and watch [Meet async/await](https://developer.apple.com/wwdc21/10132) presenting a view is basic stuff and there are many ways of doing it but your biggest issue will be expecting synchronous results when your `signUp` is likely asynchronous. How do you know the user sign up was successful? How do you know when it is done? – lorem ipsum Dec 18 '22 at 13:14

1 Answers1

0

There are a few ways to pull this off, but using a NavigationStack works really well. Something like this:

//
//  ContentView.swift
//  animation-example
//
//  Created by Andrew Carter on 12/15/22.
//

import SwiftUI

enum SignUpFlow {
    case name
    case age
    case welcome
}

struct SignUpInfo {
    var name: String
    var age: String
}

struct NameView: View {
    @Binding var name: String
    
    var body: some View {
        VStack {
            TextField("Name", text: $name)
            
            NavigationLink("Next", value: SignUpFlow.age)
                .disabled(name.isEmpty)
        }
    }
    
}

struct AgeView: View {
    @Binding var age: String
    
    var body: some View {
        VStack {
            TextField("Age", text: $age)
            
            NavigationLink("Next", value: SignUpFlow.welcome)
                .disabled(age.isEmpty)
        }
    }
    
}

struct WelcomeView: View {
    var body: some View {
        Text("Welcome")
    }
}

struct ContentView: View {
    
    @State private var path: [SignUpFlow] = []
    @State private var signUpInfo = SignUpInfo(name: "", age: "")

    var body: some View {
        NavigationStack(path: $path) {
            NavigationLink("Create Account", value: SignUpFlow.name)
                .navigationDestination(for: SignUpFlow.self) { flow in
                    switch flow {
                    case .age:
                        AgeView(age: $signUpInfo.age)
                        
                    case .name:
                        NameView(name: $signUpInfo.name)

                    case .welcome:
                        WelcomeView()
                    }
                }
        }
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

In the example above with mapView() just placed in the buttons action, it's unused because, well, it's unused. The Button closure expects a Void return, you're just making a map view than instantly throwing it away- it's not used in any way.