Search code examples
swiftxcodeswiftuiswiftui-navigationlinkswiftui-view

macOS - Error while passing information through UI View


I am trying to pass information from one SwiftUI View (let's say View1) to another one (View2) using NavigationLink. I have found this article, but when I try to run it I get the error "Cannot find type 'Event' in scope": annot find type 'Event'  in scope

Here is my code for View1:

@available(OSX 11.0, *)
struct Mods_UI: View {
    let PassMe = "Hello!"
    var body: some View {
        NavigationView {
            NavigationLink(destination: MarkdownView(item: String(PassMe))) {
                Label("Hello World!")
            }
        }
    }
}

Here is my code for View2:

struct MarkdownView: View {
    let item: Event
    
    var body: some View {
        Text(String(item))
    }
}

I have no idea what is going wrong. If more information is needed, please tell me.

Swift Version: Latest (5.3.3)

Xcode Version: Latest (12.4)


Solution

  • In the page you linked to, they have a custom type called Event, probably defined as a struct somewhere in their code that was not included in the post.

    You, however are just trying to pass a String around. You can do this:

    @available(OSX 11.0, *)
    struct Mods_UI: View {
        let passMe = "Hello!"
        var body: some View {
            NavigationView {
                NavigationLink(destination: MarkdownView(item: passMe)) {
                    Label("Hello World!")
                }
            }
        }
    }
    
    struct MarkdownView: View {
        var item: String
        
        var body: some View {
            Text(item)
        }
    }