title: how to show different alerts based on a condition after clicking a button in swiftui
body: <p>I did some research before posting it here but I was not able to fix it.</p>
  <p>In the register View I want the user to register.</p>
  <p>I created a linked list and as user registers a username my program checks whether or not the username is already taken.</p>
  <p>if it is taken it should give an alert saying that the username is already taken as the user clicks the register button.</p>
  <p>if the username is not taken then it should show an alert saying the registration is successful</p>
  <pre><code>import SwiftUI
  
  struct registerScreen: View {
  
      @State var username: String = &quot;&quot;
      @State var password: String = &quot;&quot;
      @State private var sucessfulRegister = false
      @State private var failedRegister = false
  
      var body: some View {
  
          VStack {
  
              TextField()
              SecureField()
              
              Button(action: {
  
                  let userinfo = linkedList()
  
                  if (userinfo.contains(value: self.username)){
                      // self.failedRegister = true
                      self.failedRegister.toggle()
                      // show alert that it failed
  
                  } else {
                      userinfo.insert(value: user(username: self.username, password: self.password))
                      // show alert that it is successfull
                      self.sucessfulRegister.toggle()
  
                  }
              })
  
              {
  
                  Text(&quot;Register&quot;)
                      .font(.headline)
                      .foregroundColor(.white)
                      .padding()
                      .frame(width: 220, height: 60)
                      .background(Color.green)
                      .cornerRadius(15.0)
  
              }
  
          }
  
      }
  
  }
  </code></pre>