【发布时间】:2019-12-20 21:39:10
【问题描述】:
我在navigationBarItems下创建了一个按钮,这个按钮会打开一个新的sheet,新的工作表会弹出一个窗口询问用户位置。但是,在新工作表中,CLLocationManager() 被调用了两次,定位权限弹窗会在几秒后消失。当您创建一个常规按钮时,位置弹出窗口将一直停留在那里,直到您选择其中一个选项,并且 CLLocationManager() 只会被调用一次。
代码
ConentView.swift
import SwiftUI
struct ContentView: View {
@State var show = false
@State var showEditPage = false
var body: some View {
NavigationView {
List {
Text("Text")
Button("Location button") {
print("Location button tapped")
self.show.toggle()
}.sheet(isPresented: $show) {
NewPage()
}
}
.navigationBarItems(
trailing:
VStack {
Button(action: {
print("BarItemButton tapped")
self.showEditPage.toggle()
}) {
//Top right icon
Text("BarItemButton")
}.sheet(isPresented: $showEditPage) {
//Open sheet page
NewPage()
}
}//End of trailing VStack
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
NewPage.swift
import SwiftUI
struct NewPage: View {
@ObservedObject var locationManager = LocationManager()
var body: some View {
Text("New Page")
}
}
struct NewPage_Previews: PreviewProvider {
static var previews: some View {
NewPage()
}
}
LocationManager.swift
import SwiftUI
import Foundation
import CoreLocation
import Combine
class LocationManager: NSObject, ObservableObject {
private let locationManager = CLLocationManager()
let objectWillChange = PassthroughSubject<Void, Never>()
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
print("In LocationManger.swift @initi, this is called")
}
@Published var locationStatus: CLAuthorizationStatus? {
willSet {
objectWillChange.send()
}
}
@Published var lastLocation: CLLocation? {
willSet { objectWillChange.send() }
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.locationStatus = status
print("In LocationManger.swift @Func locationManager, Status is updaing")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
self.lastLocation = location
print("Location is updating")
}
}
GitHub
您可以随意下载该项目并在笔记本电脑上试用以查看问题: Github Example Project
截图
【问题讨论】:
-
请发布您的代码,而不是期望人们下载 Xcode 项目。
-
嗨@creeperspeak,更新文本代码
标签: swift xcode navigation uinavigationbar swiftui