作者:李亞男大宝贝 | 来源:互联网 | 2023-05-17 03:14
ImtryingtoimplementsamelogicfordifferenttabsiniOSapplication,butgetThread1:EXC_BAD_
I'm trying to implement same logic for different tabs in iOS application, but get Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode 0x0). It's a simple application that should allow user to mark some points on the map(right now with annotations), and draws lines between them in process.
我正在尝试为iOS应用程序中的不同选项卡实现相同的逻辑,但获取线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码0x0)。这是一个简单的应用程序,应该允许用户在地图上标记一些点(现在带有注释),并在进程中绘制它们之间的线。
Class that contains logic:
包含逻辑的类:
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 classes look like this:
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()
}
}
I guess it's because i don't declare a delegate right. I tried to do this in my MapController class like this:
我想这是因为我没有宣布代表权利。我尝试在我的MapController类中执行此操作,如下所示:
self.Map = _Map
Map.delegate = BuildTrack.self()
, but got same exception when i clicked map(now i don't even see the map, it crashes in the init of MapController), looks like something gets disposed before time.
,但是当我点击地图(现在我甚至没有看到地图,它在MapController的init中崩溃)时得到了相同的异常,看起来像是在时间之前被处理掉的东西。
Is the problem really in delegates, and is this approach ok? When i had one ViewController and all logic was inside it, it worked fine, problem occurred when i tried to separate logic from interface.
问题是否真的在代表中,这种方法是否正常?当我有一个ViewController并且所有逻辑都在其中时,它工作正常,当我试图将逻辑与接口分开时出现问题。
1 个解决方案