0

Im trying to show another view with SwiftUI, when a button is pressed. Here is what I am trying:

import SwiftUI

struct Home: View {
    var body: some View {
        VStack {
            Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
            Button("Press here!") {
                iPhone13About()
            }
        }
    }
}

Where I hover over iPhone13About(), it says, "Result of 'iPhone13About' initializer is unused". Im a beginner with Xcode, and I am trying to mess around with Xcode and to create something.

The Swift UI View is called iPhone13.swift, and the function it is passing is

struct iPhone13About: View {

Thanks in advance!

4
  • You can present a view by using sheets, here's a tutorial. Commented Jan 24, 2022 at 0:46
  • A struct is not a function (func) Commented Jan 24, 2022 at 0:55
  • 2
    You should review Apple's SwiftUI Tutorials developer.apple.com/tutorials/swiftui. Commented Jan 24, 2022 at 0:57
  • Search for "SwiftUI NavigationView" and "SwiftUI NavigationLink". If you truly want it to be the result of a Button press, search for "SwiftUI Programatic Navigation" and remember that your destination View has to be in the hierarchy and not inside the Button's action. Commented Jan 24, 2022 at 1:15

1 Answer 1

1

try this.

struct iPhone13About: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        Button("Press to dismiss") {
            dismiss()
        }
        .font(.title)
        .padding()
        .background(Color.black)
    }
}

struct HomeView: View {

    @State private var iPhone13Flag = false

    var body: some View {
        Button("iPhone13About") {
            iPhone13Flag.toggle()
        }
        .sheet(isPresented: $showingSheet) {
            SheetView()
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It isn't working. Im trying to show the SwiftUI View iPhone13.

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.