BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.