UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 77 Milestone: Projects 13-15

Forums > 100 Days of SwiftUI


I am coding the assignment where we create notes with images. Loading an image and naming it immediatly. I created a new struct Note to store a swiftUI image and a string:

struct Note: Identifiable {
    var id: UUID
    var image: Image
    var text: String

    static var previewData: Note {
        return Note(id: UUID(), image: Image(systemName: "photo"), text: "Some note you wrote.")

After an image is picked with ImagePicker the function loadImage() is called to create a swiftUI image view:

    func loadImage() {
        guard let inputImage = inputImage else { return }
        let image = Image(uiImage: inputImage)
        newNote = Note(id: UUID(), image: image, text: "")
        showingAddNote = true
        // notes.append(newNote)

That's where I thought I could create a new note, ask for text-input from the user, wait for the input and use the dismiss-action of the edit-view to store my new note by appending it to a state variable notes (an array of Notes).

But since the function can only indirectly "show" a view by setting a boolean, how would I give the view access to the freshly created note? The idea is that the view adds whatever the user inputs as text.


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.