0

下面是代码。在这里,LocationManager 是 Observable 类,我在其中将“locationStatus”属性标记为@Published。现在,我想要实现的是当用户看到位置弹出窗口并授予位置访问权限时,它应该导航到下一个屏幕。有人可以帮我解决这个问题吗?提前致谢

 struct MyLocationScreen: View {
    
    @ObservedObject var locationManager = LocationManager()
    @State var isShowLinkTarget :Bool = false

    var body: some View {
        NavigationView{
          ZStack{
             Button(action: {

               let locStatus = self.locationManager.locationStatus

               if locStatus == .authorizedWhenInUse || locStatus == .authorizedAlways {
                   self.showLinkTarget = true
               } else {
                   self.locationManager.requestAuthorization()
                   self.locationManager.startUpdate()
               }

         }) {
             Text("Share Location")
               
            }
            NavigationLink(destination: NextScreen(), isActive: $showLinkTarget) {
                Text("")
            }
          }
        }
    }
}
4

1 回答 1

1

我无法提供逐行代码,但我可以向您展示一个您可以使用的想法。而是制作CLLocationStatus @published,将其设为 a propertyObserver,当更改将布尔 @published属性设置为 true 时,这将触发 UI 刷新,并navigation为您处理..

import SwiftUI

struct MyLocationScreen: View {
    
    @ObservedObject var locationManager = LocationManager()
    @State var isShowLinkTarget :Bool = false
    
    var body: some View {
        NavigationView{
            ZStack{
                Button(action: {
                    
                    /*
                     Once locationManager(), CLLocation property changes it's going to set @published isGranted to true in model class. Which will then trigger body refresh, and navigation can use this updated state.
                     
                     NOTE-: Below i am assigning object just as an example, this has to be your  code.
                     */
                    
                    locationManager.locationStatus = CLAuthorizationStatus()
                    
                    // You can implement below two line alone
                    
                    //                self.locationManager.requestAuthorization()
                    //                self.locationManager.startUpdate()
                    
                }) {
                    Text("Share Location")
                    
                }
                NavigationLink(destination: Text(""), isActive: $locationManager.isGranted) {
                    Text("")
                }
            }
        }
    }
}


class LocationManager: ObservableObject,Identifiable {
    
    var id  = UUID().uuidString
    @Published var isGranted:Bool = false
    
    var locationStatus:CLAuthorizationStatus?{
        didSet{
            
            // Some logic if you want like
            
//            if locationStatus == .authorizedWhenInUse || locationStatus == .authorizedAlways {
//                self.isGranted = true
//            }
//
            
            isGranted = true
        }
    }
}

class CLAuthorizationStatus{
    
}
于 2021-03-09T16:41:24.307 回答