0

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)

1 Answers1

1

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)
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Hello! This makes a lot of sense, but when I am running the script I get the error `Missing argument for parameter 'item' in call` at the bottom of `View2` (PreviewProvider). I just tried using `item: item`, and `item: String`, but they also return errors. I am not sure what to do. – stefthedoggo4 Mar 25 '21 at 15:22
  • 1
    You aren't showing the `PreviewProvider` code, which would be helpful to give you an exact answer, but that error is telling you that you need to include an `item` parameter, which is a String. You probably want `item: "Test"` or something like that -- "Test" can be whatever you want the default value to be for the preview display. – jnpdx Mar 25 '21 at 15:43