2

The HomeView()struct is not able to call in function or in button action. How should i call the HomeView() struct from another struct. While calling HomeView() either in button action or function of LoginView() , am getting "Result of 'HomeView' initializer is unused"

struct LoginView: View
{
    var body: some View
    {
    }
    func login()
    {
        HomeView() //Result of 'HomeView' initializer is unused "WARNING"
    }
}



struct LoginView: View
{
    var body: some View {
        NavigationView {
            VStack
                {
                    Button(action: {
                        HomeView() //Result of 'HomeView' initializer is unused  "WARNING"
                    })
                    {
                        HStack(alignment: .center) {
                            Spacer()
                            Text("Submit")
                        }
                    }
            }
        }
    }
}



struct HomeView: View
{
    var body: some View
    {
    }
    func submit()
    {
    }
}
3
  • HomeView() is not a function, it is constructor of value, like let view = HomeView() Commented Feb 14, 2020 at 11:29
  • What do you mean by « call a struct » ? You don’t call structs. You create them, mutate them and copy them but I don’t know what you mean by calling a struct. Commented Feb 14, 2020 at 12:33
  • @DamiaanDufaux, i updated my post Commented Feb 14, 2020 at 15:56

1 Answer 1

1

To present another view via button, please check below:

struct LoginView: View {
    @State var showingHome = false
    var loginButton: some View {
        Button(action: {
            self.showingHome.toggle()
        }) {
            Text("Login")
            .foregroundColor(Color.init(.white))
            .font(.system(size: 14))
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .background(Color.init(.systemOrange))
            .cornerRadius(4)
        }
    }

    var body: some View {
        NavigationView {
            VStack{
                loginButton.sheet(isPresented: $showingHome) {
                    HomeView()
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The user can dismiss if we present using sheet. i wanted to navigate between swiftui views
AnyView({ () -> AnyView in if user logged in { return AnyView(HomeView()) } else { return AnyView(LoginView()) } }
After a user logs in or already logged in, it will present the HomeView() otherwise it presents LoginView(). If this is what you wanted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.