gpt4 book ai didi

ios - Swift/ios/Google map - 添加 UISlider 时出现 "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

转载 作者:行者123 更新时间:2023-11-30 11:58:55 26 4
gpt4 key购买 nike

我有一个带有 GMSMapView 的导航 View Controller 。

在我的 Storyboard中,我在 MapView 上拖动了一个 UISlider,但是当我运行时,我什至看不到它。我尝试将其设置为 IBOutlet 和 IBAction,但感觉我错过了一些东西。当我运行它时没有可见的 slider 。

这是我的 Storyboard:

storyboard

这是我的代码

class CircleSizeViewController: UIViewController, GMSMapViewDelegate {

@IBOutlet weak var slider: UISlider!
@IBOutlet var mapView: GMSMapView!

let maxRadius : Double = 16093.4



var userLocation : CLLocationCoordinate2D = CLLocationCoordinate2D()

var centerMarker = GMSMarker()
var radiusMarker = GMSMarker()
var userMarker = GMSMarker()
var mRange = GMSMarker()
var isDragging = false
var isSizing = false
var isAtRange = false
var currentWidth : Double = 0.0
var currentHeading : Double = 0.0


var circ : GMSCircle = GMSCircle()



override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude: userLocation.latitude, longitude: userLocation.longitude, zoom: 15)
mapView = GMSMapView.map(withFrame: .zero, camera: camera)
view = mapView

centerMarker = GMSMarker(position: userLocation)
radiusMarker = GMSMarker(position: GMSGeometryOffset(centerMarker.position, 300, 90))
userMarker = GMSMarker(position: userLocation)
mRange = GMSMarker(position: GMSGeometryOffset(centerMarker.position, maxRadius, GMSGeometryHeading(centerMarker.position, radiusMarker.position)))

mRange.opacity = 0.0

centerMarker.icon = UIImage(named: "sizemark")
radiusMarker.icon = UIImage(named: "sizemark")
mRange.icon = UIImage(named: "sizemark")
centerMarker.isDraggable = true
radiusMarker.isDraggable = true

radiusMarker.groundAnchor = CGPoint(x:0.5, y:0.5)
centerMarker.groundAnchor = CGPoint(x:0.5, y:0.5)


mRange.map = mapView
centerMarker.map = mapView
radiusMarker.map = mapView
userMarker.map = mapView

let circleCenter = centerMarker.position
currentWidth = GMSGeometryDistance(centerMarker.position, radiusMarker.position)
circ = GMSCircle(position: circleCenter, radius: currentWidth)
circ.map = mapView

mapView.insertSubview(slider, at: 1)
mapView.delegate = self


}

任何见解将不胜感激

谢谢-T

最佳答案

您遇到一些问题。

  1. 您在 Storyboard中创建了一个 map View ,然后在代码中创建了一个新 View 。因此, slider 不是 map View 的 subview 。请参阅:mapView = GMSMapView.map(withFrame: .zero, camera: camera)

  2. 然后,您将新的 map View 设置为 View Controller 的 View ,这会从内存中删除原始 map View ,并且 slider 很弱,因此也会消失。

  3. apple docs 的注释(UIViewController.loadView()):

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

潜在的解决方案

  1. 根本不要使用 Storyboard。您可以继续使用loadView()一切都应该正常。只需在代码中创建 slider 即可。
  2. 使用 Storyboard并添加 slider 。然后覆盖viewDidLoad()并以编程方式创建 map View 并将其插入 View Controller 的 View 中 ( self.view.insert(mapView, at: 0) )

关于ios - Swift/ios/Google map - 添加 UISlider 时出现 "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47478657/

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