Home > front end >  SwiftUI how to make a view link somewhere if a condition is met after a button press, otherwise stay
SwiftUI how to make a view link somewhere if a condition is met after a button press, otherwise stay

Time:07-28

So I have this code:

import SwiftUI
struct LoginView: View {
    
    
    
    @ObservedObject var vm : PlayerViewModel
    @State private var username = ""
    private var searchAllowed : Bool{
        if(username.count>2)
        {
            return false
        }
        return true
    }
    
    
    var body: some View {
        NavigationView{
            ZStack{
                VStack{
                    Text("Enter your Username:")
                        
                    
                    
                    TextField("Username", text: $username)
                       
                    Button{
                        Task{
                            if let uuid = await vm.getUUID(username: username)
                            {
                                await vm.getPlayer(uuid: uuid)
                                print(vm.state)
                            }
                        }
                    } label:
                    {
                        LabelView()
                                
                                
                    }
                    }
                  
                    
            }
                switch vm.state{
                case .loading:
                    LoadingView()
                default:
                    EmptyView()
                }
            }
        
        }
    }
    
}

Now, inside the Button Task I call two async functions and get some data, now what I want to do is if the data I got is good(which for me I defined an enum in my ViewModel and in this case it would be good if it was vm.state.success) I want to get sent to another view like NavigationLink would do otherwise I want to just stay in the same View and maybe just display some Text View or something. But I really have no idea how I would go about implementing something like this and I've done research for a while.

If it's successful, I literally want it to be like the search button was just a navigationview and for it to send me somewhere, with a back arrow being available to send me back to the original view. But if anything else happened, I want to stay in this original view and just display some Text. Does anyone know how I can do something like this? I have been thinking about this for a while but just can't think of a solution, I need to press the button, show a loading view for a bit while an api call is loading, and if the data is good I want that button to act like it was a navigationlink to a new view otherwise if it wasn't I wanna stay in the same view and maybe just add an alert. Is this possible in SwiftUI?

CodePudding user response:

Just use the

public init(isActive: Binding<Bool>, @ViewBuilder destination: () -> Destination, @ViewBuilder label: () -> Label) initializer of the NavigationLink

Here is the usage example:

import SwiftUI
struct LoginView: View {
        
    @ObservedObject var vm : PlayerViewModel
    @State private var username = ""
    @State private var navigateToView = false
            
    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    Text("Enter your Username:")
                    
                    TextField("Username", text: $username)
                    
                    Button {
                        Task {
                            if let uuid = await vm.getUUID(username: username) {
                                await vm.getPlayer(uuid: uuid)
                                
                                DispatchQueue.main.async {
                                    navigateToView = true
                                }
                            }
                        }
                    } label: {
                        LabelView()
                    }
                }
                
                NavigationLink(isActive: $navigateToView, destination: { DestinationView() }, label: { EmptyView() })
            }
        }
    }
}

struct DestinationView: View {
    var body: some View {
        Text("You have successfully logged in!")
    }
}
  • Related