TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 77 Challenge help

Forums > 100 Days of SwiftUI

Hi. Im trying to complete day 77 challenge and maybe you could give some hints? I only have this code for now which .alert dosnt work. What do you think i could do next? This challenge is a bit overwhelming for me. :)

import SwiftData
import PhotosUI
import SwiftUI

struct ContentView: View {

    @State private var selectedItem: PhotosPickerItem?
    @State private var imageName: String = ""
    @State private var isNamingPhoto: Bool = false
    @State private var importedImage: Image?

    var body: some View {
        NavigationStack {
            VStack {

                        PhotosPicker(selection: $selectedItem) {

                            ContentUnavailableView("No picture", systemImage: "photo.badge.plus", description: Text("Tap to import a photo"))

                        }
                        .alert(isPresented: $isNamingPhoto, content: {
                            Alert(title: Text("Name the photo"), message: Text("Enter the name for the imported photo"), primaryButton: .default(Text("Save"), action: {
                                // Handle saving the image name here
                                print("Image Name: \(imageName)")
                            }), secondaryButton: .cancel())
                        })
                        .buttonStyle(.plain)

                    }
                    .navigationTitle("Name that image").font(.subheadline)

                }
            }
}

 #Preview {
            ContentView()
        }

1      

You are using an old API for .alert. Try this one

.alert("Name the photo", isPresented: $isNamingPhoto) {
    TextField("Enter Image Name", text: $imageName) // <- To enter name of photo
    Button("Done") {
        // Handle saving the image name here
        print("Image Name: \(imageName)")
    }
    .disabled(imageName.isEmpty) // <- disable the Done button until something in text field

    Button("Cancel", role: .cancel) { }
} message: {
    Text("Enter the name for the imported photo")
}

I am assume that you have a method to change the PhotosPickerItem to the importedImage when that no longer nil change the isNamingPhoto to true which will show the alert.

Remember as Paul say

Note: Don’t worry if you don’t complete challenges in the day they were assigned – in future days you’ll find you have some time to spare here and there, so challenges are something you can return back to in the future.

1      

Thanks for the .alert update :) its started to show an alert!

I have a method to load image but im not sure its final :) What do you think about it?

func loadImage() {
        Task {
            guard let imageData = try await selectedItem?.loadTransferable(type: Data.self) else { return }
            guard let importedImage = UIImage(data: imageData) else { return }

            isNamingPhoto = true

        }
    }

1      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.