1

在我一开始使用@enviroment 获取presentationMode 之后,我的init 代码似乎坏了,提示“变量'self.region' 在被初始化之前使用”

错误图像

import SwiftUI
import MapKit

struct MapView: View {
  @State private var region: MKCoordinateRegion
  @Environment(\.presentationMode) private var presentationMode
  
  init(place: Place) {
    //TOSolve: - why after using presentationmode I need underscore
    //    region = place.region
    _region = State(initialValue: place.region)
  }
  
  var body: some View {
    ZStack {
      Map(coordinateRegion: $region)
      
      VStack {
        HStack {
          Button(action: { presentationMode.wrappedValue.dismiss()  }) {
            Image(systemName: "xmark.circle.fill")
              .imageScale(.large)
          }
          Spacer()
        }
        .padding()
        Spacer()
      }
    }
    .edgesIgnoringSafeArea(.all)
    .navigationBarHidden(true)
  }
}
4

0 回答 0