SwiftUI triggering action on button without user clicking on button

Viewed 442

I have created a very simple login view. enter image description here

I am observing a very weird behavior. When I click inside text boxes, that click ends up triggering "action" on "Login" button. Following is code that is responsible for setting up this part of the view.

var body: some View{
        ZStack{
            VStack(alignment: .center){
                TextField("Login", text: $model.userName).padding().border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: 1).cornerRadius(2.0)
                SecureField("Password",text:$model.password).padding().border(Color.black, width:1).cornerRadius(2.0)
                Toggle(isOn:$model.rememberMe){
                    Text("Remember me")
                }
                if (model.loginFailed){
                    Text(model.errorMessage!).font(.footnote).foregroundColor(.red).animation(.easeIn)
                }
                VStack{
                    Button(action: signIn) {
                        Text("Login")
                    }
                    Button(action:recoverPassword){
                        Text("Forgot Password?")
                    }
                }
                }.padding().zIndex(100).disabled(model.loginInProgress)
            LoginInProgressIndicator().animation(.easeIn).zIndex(model.loginInProgress ? 200: 0).opacity(model.loginInProgress ? 0.9 : 0)
        }
    }

Some time "action" on button triggers on first click inside either of text boxes. Sometime it triggers after i try few attempts with clicking inside text boxes. Bottom line is that "action" on "login" button is getting triggered without having to click on the button.

Is there something wrong with the way code is set up. Or something special needs to be done to handle "action" on buttons when they are part of a group or something.

Any help will be appreciated.

Update: I added some old school diagnostics to check behavior of "action" for two buttons in the view. It turned out that clock inside text box, triggeed action on both buttons. Print messages always came in pair.

More Update The problem is more widespread. Click inside anywhere in toplevel VStack results in action trigger on the button.

1 Answers

The problem is with having a Form element surrounding that section. I have an enclosure view that uses Form. This login input section view was inclosed inside it. Behavior of Form seem to be that no matter where you click inside that form, it triggers all the button action enclosed inside it. That really is not what I would have expected.

Related