SwiftUIのTextfieldやPickerに入力された任意の文字列や画像を、MapkitのUILabelに表示したいです。
文字列や画像の格納された変数を受渡するために@Stateや@Bindingなどを使って下記のコードのように実現しようとしたのですが、「Variable 〇〇 Used by function definition before being initialized」というエラーが出てしまい、意図した動作ができませんでした。
エラーの内容から初期化処理に関するエラーであると推測したのですが、具体的なエラーの内容の理解と、どのような書き方をすれば解決出来るか分からないので、ご教授いただきたいです。
よろしくお願いいたします。
swift
import SwiftUI import MapKit struct MapView: UIViewRepresentable { func makeUIView(context: Context) -> MKMapView { let map = MKMapView() map.delegate = context.coordinator map.addGestureRecognizer(context.coordinator.myLongPress) return map } func makeCoordinator() -> Coordinator { Coordinator(self) } func updateUIView(_ mapView: MKMapView, context: Context) { let latitude = 43.31513, longitude = 143.08874 let coordinate = CLLocationCoordinate2DMake(latitude, longitude) // 縮尺を設定 let span = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5) // マップの中心を設定 let region = MKCoordinateRegion(center: coordinate, span: span) mapView.setRegion(region, animated: true) } class Coordinator: NSObject, MKMapViewDelegate { var parent: MapView let myLongPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer() init(_ parent: MapView) { self.parent = parent super.init() self.myLongPress.addTarget(self, action: #selector(recognizeLongPress)) } @objc func recognizeLongPress(sender: UILongPressGestureRecognizer) { if sender.state == .ended { if let mapView = sender.view as? MKMapView { // タップした位置を取得 let point = sender.location(in: mapView) // mapView上での位置に変換 let coordinate = mapView.convert(point, toCoordinateFrom: mapView) print(coordinate.latitude) print(coordinate.longitude) print("-") // アノテーション作成 let annotation = MKPointAnnotation() annotation.coordinate = coordinate //annotation.title = "taitooooru" mapView.addAnnotation(annotation) } } } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { @Binding var num:Int @Binding var titlename:String @Binding var image: UIImage? //@State var num:Int = 1 //@State var titlename:String = "ssss" let identifier = "annotation" if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { annotationView.annotation = annotation return annotationView } else { let annotationView = MKAnnotationView( annotation: annotation, reuseIdentifier: identifier ) //ピンのスタイルを変更 if num == 1{ annotationView.image = UIImage(named: "pre") }else if num == 2{ annotationView.image = UIImage(named: "Eat") } //stackViewの設定 let stackView = UIStackView() stackView.axis = NSLayoutConstraint.Axis.vertical stackView.alignment = UIStackView.Alignment.leading //ラベルの作成 let title = UILabel() title.text = "\(titlename)" title.font = UIFont.boldSystemFont(ofSize: 20) title.isUserInteractionEnabled = true stackView.addArrangedSubview(title) //景色の画像の作成 let imageView = UIImageView(image: UIImage(named: "Try")) imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) imageView.contentMode = .scaleAspectFit stackView.addArrangedSubview(imageView) annotationView.canShowCallout = true annotationView.detailCalloutAccessoryView = stackView annotationView.isUserInteractionEnabled = true return annotationView } } } }
swift
import SwiftUI struct uiview: View { @State var titlename:String = "" @State var honbun:String = "" @State private var image: UIImage? @State var input:Bool = false @State var num:Int = 1 var body: some View { ZStack{ MapView() VStack{ Button(action: { //ボタンを押した時の処理を記載 input = true }) { Text("ピン設定のためのボタン") .foregroundColor(.black) } } .sheet(isPresented: $input) { inputView(titlename: self.$titlename, honbun: self.$honbun, image: self.$image, num: $num) } } } }
swift
import SwiftUI struct inputView: View { @Binding var titlename:String @Binding var honbun:String @Binding var image: UIImage? @Binding var num:Int @State var showingImagePicker:Bool = false @Environment(\.presentationMode) var presentationMode private func didTapDismissButton() { presentationMode.wrappedValue.dismiss() } var body: some View { ZStack{ NavigationView { VStack { Form { TextField("ピンの名前", text: $titlename) Picker(selection: $num, label: Text("選択")) { Text("みかん").tag(1) Text("ぶどう").tag(2) } .frame(width: 400) Button(action: { showingImagePicker = true }) { Text("フォトライブラリから選択") if let uiImage = image { Image(uiImage: uiImage) .resizable() .scaledToFit() .frame(width: 200, height: 200) } else { Image("noimage") .resizable() .scaledToFit() .frame(width: 200, height: 200) } } Button(action: { //確定ボタンを押した時の処理を記載 didTapDismissButton() }) { Text("確定") } } .navigationBarTitle("ピン情報入力画面") } } .sheet(isPresented: $showingImagePicker) { ImagePicker(sourceType: .photoLibrary, selectedImage: $image) } } } }
まだ回答がついていません
会員登録して回答してみよう