90888


TabViewの子ビュー内にてPHPickerを使用し、PHPicker使用後に元々開いていた子ビューではなく、TabView内のStateで指定している最初のTabに戻ってしまいます。

PHPickerを閉じる際には、下記コードのようにdismissを用いて画面を閉じる操作をしており、どのようにしたら元の画面に戻れるのかを模索しています。

お分かりになる方がいれば、ご教示くださいませ。

ContentView:

[code]

    struct ContentView: View {
    
    enum Tabs: String {
        case tab1 = "FirstView"
        case tab2 = "SecondView"
        case tab3 = "アカウント"
    }
    
    func returnNavigationTitle(tabSelection: Tabs) -> String {
        switch tabSelection {
        case .tab1:
            return "FirstView"
        case .tab2:
            return "SecondView"
        case .tab3:
            return "アカウント"
        }
    }
    
    @State private var navigationTitle: String = Tabs.tab1.rawValue
    @State var tabSelection: Tabs = .tab1
    
    var body: some View {
        TabView(selection: $tabSelection) {
            FirstView()
                .tabItem {
                    Image(systemName: "house.fill")
                    Text(Tabs.tab1.rawValue)
                }.tag(1)//.tabItem
            SecondView()
                .tabItem {
                    Image(systemName: "list.bullet")
                    Text(Tabs.tab2.rawValue)
                }.tag(2)//.tabItem
            PhotoPickerView()
                .tabItem {
                    Image(systemName: "person.circle")
                    Text(Tabs.tab3.rawValue)
                }.tag(3)//.tabItem
        }.accentColor(.blue)
    }
    
[/code]

ImagePicker:

[code]

    struct ImagePicker: UIViewControllerRepresentable {
    
    @Environment(\.presentationMode) private var presentationMode
    var sourceType: UIImagePickerController.SourceType = .photoLibrary
    @Binding var selectedImage: UIImage
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        
        let imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = true
        imagePicker.sourceType = sourceType
        imagePicker.delegate = context.coordinator
        
        return imagePicker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
        
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
        var parent: ImagePicker
        
        init(_ parent: ImagePicker) {
            self.parent = parent
        }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
            if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
                parent.selectedImage = image
            }
            
            parent.presentationMode.wrappedValue.dismiss()
        }
        
    }
    
[/code]

PhotoPickerView:

[code]

    struct PhotoPickerView: View {
    
    @State private var isShowingPhotoPicker = false
    @State private var avaterImage = UIImage(named: "default-avatar")!
    
    var body: some View {
        VStack {
            Image(uiImage: avaterImage)
                .resizable()
                .scaledToFit()
                .frame(width: 150, height: 150)
                .clipShape(Circle())
                .padding()
                .onTapGesture {
                    isShowingPhotoPicker = true
                }
            
            Spacer()
        }
        .navigationTitle("Profile")
        .sheet(isPresented: $isShowingPhotoPicker, content: {
            ImagePicker(sourceType: .photoLibrary, selectedImage: self.$avaterImage)
            //PhotoPicker(avaterImage: $avaterImage)
        })
    }
    
[/code]

開発環境：  
Xcode 13.3  
Swift 5


CC BY-SA 4.0
2022-05-12T07:06:20.957
88779
2022-09-01T19:00:48.257
2022-05-12T09:41:49.980
52581
52581
question
swift swiftui swift5
SwiftUIにてPHPickerを用いた際に、PHPickerを開いたTabに戻りたいのだが、初期のTabView画面に戻ってしまう。


tagの設定が間違っています。  
TabViewのselectionと.tag()の型は揃えないといけません。

[code]

    .tag(Tabs.tab1)
    
[/code]


CC BY-SA 4.0
2022-09-01T19:00:48.257
90888
2022-09-01T19:00:48.257
40856
88779
answer