我试图为iOS应用程序中的不同选项卡实现相同的逻辑,但得到了线程1:EXC_BAD_指令(代码=EXC_1386_INVOP,子代码0x0)。这是一个简单的应用程序,应该允许用户在地图上标记一些点(现在用注释),并在它们之间画线。
包含逻辑的类:
class MapController : NSObject, MKMapViewDelegate{
var Map: MKMapView!
var points : [CGPoint]
init(_Map : MKMapView!, delClass : String)//, coder aDecoder: NSCoder)
{
self.Map = _Map
points = [CGPoint]()
self.Map.mapType = MKMapType.Satellite
let centre = CLLocationCoordinate2D(latitude: 40.0000000,
longitude: 49.0000000)
let span = MKCoordinateSpan(latitudeDelta: 10.01,
longitudeDelta: 10.01)
let region = MKCoordinateRegion(center: centre, span: span)
self.Map.setRegion(region, animated: false)
self.Map.regionThatFits(region)
let urlTemplate = "http://someip/mapcache/tms/1.0.0/test@GoogleMapsCompatible/{z}/{x}/{y}.png"
let carte_indice = MKTileOverlay(URLTemplate: urlTemplate)
carte_indice.geometryFlipped = true
carte_indice.canReplaceMapContent = false
print("Map")
self.Map.addOverlay(carte_indice)
}
func longPressGesture()
{
let lpg = UILongPressGestureRecognizer(target: self.Map, action: "longPressAction:")
lpg.minimumPressDuration = 1;
Map.addGestureRecognizer(lpg)
}
func longPressAction(myRecognizer : UILongPressGestureRecognizer)
{
let currPoint = myRecognizer.locationInView(Map)
let point = Map.convertPoint(currPoint, toCoordinateFromView: Map)
points.append(currPoint);
if(points.count>1)
{
let startPoint = Map.convertPoint(points[points.count-2], toCoordinateFromView: Map)
let endPoint = Map.convertPoint(currPoint, toCoordinateFromView: Map)
var lineCoords = [startPoint,endPoint]
var line = MKPolyline(coordinates: &lineCoords, count: 2)
var test = MKPolylineRenderer(polyline: line)
test.lineWidth = 10;
test.strokeColor = UIColor.redColor()
Map.addOverlay(line)
}
let myAnnotation = MKPointAnnotation();
myAnnotation.coordinate = point
myAnnotation.title = "Test"
myAnnotation.subtitle = "Test subtitle"
Map.addAnnotation(myAnnotation);
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay);
circle.strokeColor = UIColor.redColor();
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1);
circle.lineWidth = 1;
return circle;
}else if overlay is MKTileOverlay {
var carte_Renderer = MKTileOverlayRenderer(overlay: overlay)
carte_Renderer.alpha = 0.9
return carte_Renderer
}else if overlay is MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay);
polylineRenderer.strokeColor = UIColor.blueColor();
polylineRenderer.lineWidth = 5;
return polylineRenderer;
}else {
return MKPolylineRenderer();
}
}
}
ViewController类如下所示:
class BuildTrack: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
@IBOutlet var testRef: MKMapView!
var mapController : MapController!
required init?(coder aDecoder : NSCoder)
{
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
mapController = MapController(_Map: testRef, delClass: "BuildTrack")
mapController.longPressGesture();
testRef.delegate = mapController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我想是因为我没有声明一个代表是对的。我试图在MapController类中这样做:
self.Map = _Map
Map.delegate = BuildTrack.self()
,但是当我单击map时出现了同样的异常(现在我甚至都看不到这个map,它在MapController的in i t中崩溃了),看起来有些东西在时间之前就被释放了。
代表们真的有问题吗?这种方法行吗?当我有一个ViewController并且所有的逻辑都在里面时,它工作得很好,当我试图将逻辑和接口分离时出现了问题。
最佳答案
我发现在它有价值之前使用outlettestRef
有问题
尝试在!
声明中添加mapController
var mapController: MapController!
并从init中移除初始化:
required init?(coder aDecoder : NSCoder) {
super.init(coder: aDecoder)
}