0

There is a code that, by pressing the "Sign Up" button at the bottom of the screen, should open another window. But for some reason, when you click on the button, no action occurs.

I need the View to switch when I click on the button.

import SwiftUI
import CoreData

struct LoginView: View {
    @State private var email: String = "Email"
    @State private var password: String = "Password"
    @State private var showSignup: Bool = false
    
    var body: some View {
        ZStack {
            VStack {
                VStack(alignment: .leading) {
                    VStack {
                        LoginText
                        
                        LoginForm
                        
                        GradientButton(text: "Login")
                            .frame(maxWidth: .infinity, alignment: .leading)

                        
                        ForgotPass(text: "Forgor Password")
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                }
                .padding([.top, .horizontal], 40)
                .frame(maxWidth: .infinity, alignment: .leading)
                .offset(y: 104)
                
                VStack {
                    Spacer()
                    Rectangle()
                        .frame(height: 1)
                        .foregroundColor(.secondary.opacity(0.4))
                    
                    VStack {
                        Button( action: {
                            showSignup.toggle()
                        }, label: {
                            ForgotPass(text: "Sign Up")
                        })
                    }
                }
            }
            .background(Color.white)
            if showSignup {
                SignupView()
            }
        }
    }
  • When you init signup to true , what happens ? – Ptit Xav Feb 21 '22 at 19:00
  • It works for me. Are you sure your other views are not messing with the SignupView? – HunterLion Feb 21 '22 at 19:01
  • What are LoginText and LoginForm ? Seems that some () are missing. Check also this [post](https://stackoverflow.com/questions/56797333/swiftui-change-view-with-button/56798708#56798708) – Ptit Xav Feb 21 '22 at 19:06
  • @PtitXav If you manually change false to true, then everything works. But through the button and showSignup.toggle() no! Login Text and LoginForm work, they are variables. Everything is fine with them and there are no mistakes. showSignup.toggle() does not work when I click on the button. – Не человек и не кот Feb 21 '22 at 19:19
  • @HunterLion If you launch the preview and click on Sign up at the bottom of the screen, nothing happens, although another menu should appear. – Не человек и не кот Feb 21 '22 at 19:22
  • Too much going on in that View, break it up based on the @ States – malhal Feb 21 '22 at 19:45

1 Answers1

0

It's working for me without any issues. May be something to do with your other part of view hierarchy. I observed this toggle' behavior, even it didn't work for me once but I am sure root cause is only due to the way navigation and view hierarchy is implemented.

Actually you implement navigation below code should work without any issues (During dismissal of SignupView showSignup boolean always gets reset to false).

Button(action: {
       showSignup = true
}, label: {
       ForgotPass(text: "Sign Up")
 })

If you don't want to use navigation then toggle should work. If toggle is still causing a problem for you then alternatively you can try this (I tried and its working fine)

Button(action: {
       showSignup = !showSignup
}, label: {
       ForgotPass(text: "Sign Up")
})

Finally you still facing issues then root cause might be with other part of view hierarchy and navigation.

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Srivathsa
  • 606
  • 10
  • 34