GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SwiftUI PencilKit Navigation and UIViewControllerRepresentables

Forums > SwiftUI

Rather than selecting from a list, I'm trying to navigate between drawings like a book using buttons to cycle through, but the canvas doesn't update.

I'm following the great tutorial by DevTechie at https://www.youtube.com/watch?v=amZH2i6l004&list=PLbrKvTeCrFAfoACvHOPWFmDIaKUqBZgEr&index=5

The github is here https://github.com/devtechie/DrawingDocuments

Here's my ContentView. I thought changing the id parameter would change the drawing displayed on the canvas. But it seems like DrawingWrapper isn't updating.

DrawingWrapper(UIViewControllerRepresentable) uses a DrawingManager to pull from CoreData and DrawingViewController to define a PKCanvas.

struct ContentView: View {
  @StateObject var manager = DrawingManager()
  @State var addNewShown = false
  @State var pageNumber: Int = 0
  @State var newVar = UUID()

  var body: some View {
    VStack{
      Text(manager.docs[pageNumber].name!)
      HStack{
        Button(action:{
          pageNumber -= 1
          newVar = manager.docs[pageNumber].id!
        }){
          Image(systemName: "chevron.left")
        }
        Spacer()
        Button(action:{
          pageNumber += 1
          newVar = manager.docs[pageNumber].id!
        }){
          Image(systemName: "chevron.right")
        }
      }

      DrawingWrapper(manager: manager, id: $newVar)

    }
  }
 }

3      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.