gpt4 book ai didi

ios - 如果我直接实例化 VC,setRegion() 不起作用,但如果我通过 segue Swift 4 访问 VC,setRegion() 则不起作用

转载 作者:行者123 更新时间:2023-11-29 05:11:38 26 4
gpt4 key购买 nike

我正在尝试一个函数行为的奇怪的(在我看来)改变,这取决于我如何到达包含它的 VC。当用户点击远程通知操作时,应用程序从后台转到前台,转到NewMapViewController ,加载一条路线,将其显示为 MKPolyline并根据MKPolyline设置区域MapRect 。现在我面临的问题是setRegion()不起作用取决于我如何到达 NewMapViewController 。如果在操作中我实例化主 UINavigationController并访问 NewMapController通过 segue 比 setRegiondisplayRoute()按预期工作,但我看到 loginVC在展示之前。所以不显示loginVC我改为实例化 NewMapViewController直接,但这样做setRegiondisplayRoute()无法放大MKPolyline不再了。为什么会发生这种情况?起初我以为configureMap (以用户位置为中心的 map )被调用但没有,所有打印显示当 NewMapViewController当计数器加载到 1 时,它永远不会被调用。我尝试使用 setVisibleMapRect()而不是setRegion()但这没有什么区别。这是一个已知的错误吗?还有其他方法可以到达 newMapViewController我应该尝试吗?一如既往,非常感谢您的时间和帮助。

更新:

经过几次尝试,我决定调用 checkAlerts()直接来自 Action 体,而不是来自 viewWillApper()柜台检查。添加 MKPolyline 时导致零错误在displayRoute() 。搜索了一下我发现这是一个线程问题,因此将其添加到 DispatchQueue.main.async 中排队一切再次按预期工作。无论我调用 checkAlerts() 都有效来自 Action 主体或来自 viewWillApper()柜台检查。看来当我实例化UINavigationController时并转到NewMApController通过segue,它有时间加载路线并计算折线,但是直接实例化时NewMapController才不是。我想更好地理解它,这对您来说有意义吗?

这是操作:

case Id.checkActionIdentifier:
print("Check tapped")
let userInfo = response.notification.request.content.userInfo
if let routeToCheck = userInfo[NSLocalizedString("Route", comment: "")] as? String {
RoutesArray.routeName = routeToCheck
// print("rotta is: \(routeToCheck)")
}
// let content = response.notification.request.content
// print(" Body \(content.body)")
// print(" Title \(content.title)")
NewMapViewController.routeCheckCounter = 1

// Goes to NewMapViewController but showing the loginVc first
// let storyboard = UIStoryboard(name: "Main", bundle: nil)
// let initialViewController : UINavigationController = storyboard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
// self.window?.rootViewController = initialViewController
// self.window?.makeKeyAndVisible()
// let vc = initialViewController.viewControllers[0]
// vc.performSegue(withIdentifier: "alertToMapSegue", sender: nil)

// setRegion in displayRoute() not working ???
if let navigationVC = self.window?.rootViewController as? UINavigationController {
let main = UIStoryboard(name: "Main", bundle: nil)
let newMapVC = main.instantiateViewController(withIdentifier: "NewMapViewController") as! NewMapViewController
navigationVC.pushViewController(newMapVC, animated: true)
}

这是显示 MKPolyline 的函数:

func displayRoute() {
print("displayRoute called")
var coordinateArray: [CLLocationCoordinate2D] = []
for point in MapArray.actualRouteInUseCoordinatesArray {
let location = CLLocationCoordinate2D(latitude: point.coordinate.latitude, longitude: point.coordinate.longitude)
coordinateArray.append(location)
}
// tracked CLLocations route
routePolyline = MKGeodesicPolyline(coordinates: coordinateArray, count: coordinateArray.count)
mapView.add(routePolyline, level: MKOverlayLevel.aboveRoads)
let startRouteAnnotation: RouteAnnotation = RouteAnnotation(title: "Route Start", coordinate: MapArray.actualRouteInUseCoordinatesArray.first!.coordinate)
self.mapView.addAnnotation(startRouteAnnotation)

let endRouteAnnotation = RouteAnnotation(title: "Route End", coordinate: MapArray.actualRouteInUseCoordinatesArray.last!.coordinate)
self.mapView.addAnnotation(endRouteAnnotation)

print("MapArray.actualRouteInUseCoordinatesArray.count is: \(MapArray.actualRouteInUseCoordinatesArray.count)")


// full display
let region = MKCoordinateRegionForMapRect(routePolyline!.boundingMapRect)
mapView.setRegion(region, animated: true)

// padded in
// self.mapView.setRegion(MKCoordinateRegionForMapRect(padMapRect(rect: routePolyline.boundingMapRect)), animated: true)
}

这就是我如何调用 viewWillAppear() 处的函数:( checkAlerts() 比调用 displayRoute()

// if loading vc from an scheduled alert check notification
print(" CheckCounter is: \(NewMapViewController.routeCheckCounter ?? 0)")
if NewMapViewController.routeCheckCounter ?? 0 > 0 {
print("checkAlerts called in viewDidLoad")
// load the route and call displayRoute()
checkAlerts()
} else {
// just center map on user location
configureMap()
return
}

最佳答案

在我的问题的更新部分中描述的尝试之后,这里是修改后的函数,其工作方式正如预期的那样:

func displayRoute() {
print("displayRoute called")
var coordinateArray: [CLLocationCoordinate2D] = []
for point in MapArray.actualRouteInUseCoordinatesArray {
let location = CLLocationCoordinate2D(latitude: point.coordinate.latitude, longitude: point.coordinate.longitude)
coordinateArray.append(location)
}
DispatchQueue.main.async {
// tracked CLLocations route
self.routePolyline = MKGeodesicPolyline(coordinates: coordinateArray, count: coordinateArray.count)
self.mapView.add(self.routePolyline, level: MKOverlayLevel.aboveRoads)

let startRouteAnnotation: RouteAnnotation = RouteAnnotation(title: "Route Start", coordinate: MapArray.actualRouteInUseCoordinatesArray.first!.coordinate)
self.mapView.addAnnotation(startRouteAnnotation)

let endRouteAnnotation = RouteAnnotation(title: "Route End", coordinate: MapArray.actualRouteInUseCoordinatesArray.last!.coordinate)
self.mapView.addAnnotation(endRouteAnnotation)

print("MapArray.actualRouteInUseCoordinatesArray.count is: \(MapArray.actualRouteInUseCoordinatesArray.count)")
// self.mapView.setRegion(MKCoordinateRegionForMapRect(padMapRect(rect: routePolyline.boundingMapRect)), animated: true)

self.mapView.setVisibleMapRect(self.padMapRect(rect: self.routePolyline.boundingMapRect), animated: true)
}
}

关于ios - 如果我直接实例化 VC,setRegion() 不起作用,但如果我通过 segue Swift 4 访问 VC,setRegion() 则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59626552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com