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!

  • You can present a view by using sheets, [here's a tutorial](https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets). – EmilioPelaez Jan 24 '22 at 00:46
  • A `struct` is not a function (`func`) – George Jan 24 '22 at 00:55
  • 2
    You should review Apple's SwiftUI Tutorials https://developer.apple.com/tutorials/swiftui. – workingdog support Ukraine Jan 24 '22 at 00: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. – jnpdx Jan 24 '22 at 01:15

1 Answers1

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()
        }
    }
}
bdeviOS
  • 449
  • 1
  • 6