自定义地图视图在swift中的initWithCoder中崩溃

时间:2015-05-08 05:06:01

标签: swift ios8 mkmapview mapkit exc-bad-access

我有一个UITableViewCell,它有一个地图视图作为子视图。创建了一个Custom类,它处理所有操作,如添加注释,委托等,并将其命名为 CustomeMap.swift

我在 Cell Xib 中将地图视图的班级名称更改为CustomeMap后,应用程序在initWithCoder CustomeMap方法崩溃了几次,因为我没有实施那种方法。后来我添加了如下方法:

import UIKit
import MapKit


protocol CustomeMapDelegate{
    func annotationClickEvent(info:NSDictionary?)
}

class CustomeMap: MKMapView,MKMapViewDelegate,CLLocationManagerDelegate {

    var locationManager:CLLocationManager?
    var delegate1:CustomeMapDelegate?
    var tap:UITapGestureRecognizer?

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib()
    {
        if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways {
            self.showsUserLocation = true;
        }
        else
        {
            locationManager = CLLocationManager()
            locationManager?.delegate = self;
            if locationManager!.respondsToSelector("requestAlwaysAuthorization")
            {
                locationManager?.requestAlwaysAuthorization()
            }
        }
        self.delegate = self
        self.userInteractionEnabled = true
        tap = UITapGestureRecognizer(target: self, action: "calloutAction:")
    }
    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
    {
        if(status == CLAuthorizationStatus.AuthorizedAlways)
        {
            self.showsUserLocation = true;
        }
    }

    func skipTouch()
    {

    }

    class func createAnnotationForDict(temp:NSDictionary)->MyPointAnnotation
    {
        var venue = temp.valueForKey("venue") as? NSDictionary
        var lat = venue!.valueForKey("latitude") as! Double
        var lon = venue!.valueForKey("longitude") as! Double
        var venueName = venue!.valueForKey("name") as! String
        var dealName = temp.valueForKey("title") as? String

        var coord:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
        coord.latitude = lat
        coord.longitude = lon
        var point = MyPointAnnotation()
        point.userInfo = temp
        point.coordinate = coord
        point.title = dealName
        point.subtitle = venueName
        return point
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
    {
        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red

            var assImg = UIImageView(image: UIImage(named: "r_arrow_g"))
            assImg.frame = CGRectMake(0, 0, 16, 16)
            assImg.contentMode = UIViewContentMode.ScaleAspectFit
            pinView!.rightCalloutAccessoryView = assImg

        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
    }
    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
    {
        if view.annotation.isKindOfClass(MKUserLocation){ return }
        if let t1 = tap
        {

        }
        else
        {
            tap = UITapGestureRecognizer(target: self, action: "calloutAction:")
        }
        view.addGestureRecognizer(tap!)
    }
    func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
    {
        if view.annotation.isKindOfClass(MKUserLocation){ return }
        view.removeGestureRecognizer(tap!)
    }
    func calloutAction(sender:UITapGestureRecognizer)
    {
        if(delegate1 != nil)
        {
            var view = sender.view as! MKAnnotationView
            var ann = view.annotation as! MyPointAnnotation
            delegate1!.annotationClickEvent(ann.userInfo)
        }
    }

}

但仍然是同样的问题。

  

崩溃日志

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x00000001 


    Thread : Crashed: com.apple.main-thread
0  libGPUSupportMercury.dylib     0x2b7228fe gpus_ReturnNotPermittedKillClient
1  libGPUSupportMercury.dylib     0x2b7233cb gpusSubmitDataBuffers
2  libGPUSupportMercury.dylib     0x2b723249 gldCreateContext
3  GLEngine                       0x2717191b gliCreateContextWithShared
4  OpenGLES                       0x2724dab3 -[EAGLContext initWithAPI:properties:] + 406
5  OpenGLES                       0x2724d86f -[EAGLContext initWithAPI:sharedWithCompute:] + 142
6  VectorKit                      0x2fc2d58b ggl::OESContext::OESContext(ggl::GLDevice*, std::__1::shared_ptr<ggl::OESSharegroup>) + 530
7  VectorKit                      0x2fc2663f ggl::GLDevice::createRenderer() + 110
8  VectorKit                      0x2fb46c0f -[MDDisplayLayer _createGLLayer] + 166
9  VectorKit                      0x2fb469af -[MDDisplayLayer init] + 70
10 VectorKit                      0x2f8763b7 -[VKMapView initWithGlobe:shouldRasterize:inBackground:] + 486
11 MapKit                         0x25f4937f -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 362
12 MapKit                         0x25f7b14b -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:] + 982
13 MapKit                         0x25f7bc4d -[MKMapView initWithCoder:] + 128
14 AppName                        0x000ce410 @objc AppName.CustomeMap.init (AppName.CustomeMap.Type)(coder : ObjectiveC.NSCoder) -> AppName.CustomeMap (CustomeMap.swift)
15 UIKit                          0x280f6611 -[UIClassSwapper initWithCoder:] + 192
16 UIKit                          0x281bd6ef UINibDecoderDecodeObjectForValue + 850
17 UIKit                          0x281bd38f -[UINibDecoder decodeObjectForKey:] + 334
18 UIKit                          0x280f6253 -[UIRuntimeConnection initWithCoder:] + 150
19 UIKit                          0x281bd6ef UINibDecoderDecodeObjectForValue + 850
20 UIKit                          0x281bd645 UINibDecoderDecodeObjectForValue + 680
21 UIKit                          0x281bd38f -[UINibDecoder decodeObjectForKey:] + 334
22 UIKit                          0x280f593f -[UINib instantiateWithOwner:options:] + 958
23 UIKit                          0x28014077 -[UIViewController _loadViewFromNibNamed:bundle:] + 238
24 UIKit                          0x27e52e99 -[UIViewController loadView] + 92
25 UIKit                          0x27d370ed -[UIViewController loadViewIfRequired] + 68
26 UIKit                          0x27de0ed5 -[UINavigationController _layoutViewController:] + 32
27 UIKit                          0x27de0dfd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
28 UIKit                          0x27de0393 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
29 UIKit                          0x27de00c3 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
30 UIKit                          0x27ddfe2d -[UINavigationController __viewWillLayoutSubviews] + 44
31 UIKit                          0x27ddfdc1 -[UILayoutContainerView layoutSubviews] + 184
32 UIKit                          0x27d347ff -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
33 QuartzCore                     0x2775a835 -[CALayer layoutSublayers] + 136
34 QuartzCore                     0x2775620d CA::Layer::layout_if_needed(CA::Transaction*) + 360
35 QuartzCore                     0x27756095 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
36 QuartzCore                     0x27755a71 CA::Context::commit_transaction(CA::Transaction*) + 224
37 QuartzCore                     0x27755875 CA::Transaction::commit() + 324
38 UIKit                          0x27d2cc91 _afterCACommitHandler + 132
39 CoreFoundation                 0x247e0ffd __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
40 CoreFoundation                 0x247de6bb __CFRunLoopDoObservers + 278
41 CoreFoundation                 0x247deac3 __CFRunLoopRun + 914
42 CoreFoundation                 0x2472c3b1 CFRunLoopRunSpecific + 476
43 CoreFoundation                 0x2472c1c3 CFRunLoopRunInMode + 106
44 GraphicsServices               0x2bd59201 GSEventRunModal + 136
45 UIKit                          0x27d9643d UIApplicationMain + 1440
46 AppName                        0x000c1d68 main (AppDelegate.swift:17)

删除了Subclassing概念,只创建了一个视图并添加了MKMapView作为子视图。但现在我看到了崩溃

  

崩溃日志

Thread : Crashed: com.apple.main-thread
 0  libGPUSupportMercury.dylib     0x2fc468fe gpus_ReturnNotPermittedKillClient
 1  libGPUSupportMercury.dylib     0x2fc473cb gpusSubmitDataBuffers
 2  libGPUSupportMercury.dylib     0x2fc47249 gldCreateContext
 3  GLEngine                       0x2b87393b gliCreateContextWithShared
 4  OpenGLES                       0x2b94fab3 -[EAGLContext initWithAPI:properties:] + 406
 5  OpenGLES                       0x2b94f86f -[EAGLContext initWithAPI:sharedWithCompute:] + 142
 6  VectorKit                      0x33ffde8b ggl::OESContext::OESContext(ggl::GLDevice*, std::__1::shared_ptr<ggl::OESSharegroup>) + 530
 7  VectorKit                      0x33ff6e77 ggl::GLDevice::createRenderer() + 110
 8  VectorKit                      0x33f1399b -[MDDisplayLayer _createGLLayer] + 166
 9  VectorKit                      0x33f1373b -[MDDisplayLayer init] + 70
 10 VectorKit                      0x33c3eae3 -[VKMapView initWithGlobe:shouldRasterize:inBackground:] + 486
 11 MapKit                         0x2a6586d7 -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 362
12 MapKit                         0x2a68a67f -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:] + 1018
13 MapKit                         0x2a676b83 -[MKMapView initWithFrame:] + 130
14 UIKit                          0x2c44df19 -[UIView init] + 44
15 AppName                        0x000f7598 @!objc ObjectiveC.MKMapView.init (ObjectiveC.MKMapView.Type)() -> ObjectiveC.MKMapView (CustomeMap.swift)
16 AppName                        0x000f4f1c ObjectiveC.MKMapView.__allocating_init (ObjectiveC.MKMapView.Type)() -> ObjectiveC.MKMapView (CustomeMap.swift)
17 AppName                        0x000ef614 AppName.CustomeMap.awakeFromNib (AppName.CustomeMap)() -> () (CustomeMap.swift:31)
18 AppName                        0x000f06ec @objc AppName.CustomeMap.awakeFromNib (AppName.CustomeMap)() -> () (CustomeMap.swift)
19 UIKit                          0x2c7ee561 -[UINib instantiateWithOwner:options:] + 1680
20 UIKit                          0x2c70d303 -[UIViewController _loadViewFromNibNamed:bundle:] + 238
21 UIKit                          0x2c54c805 -[UIViewController loadView] + 92
22 UIKit                          0x2c430af9 -[UIViewController loadViewIfRequired] + 68
23 UIKit                          0x2c4dab75 -[UINavigationController _layoutViewController:] + 32
24 UIKit                          0x2c4daa9d -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
25 UIKit                          0x2c4da033 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
26 UIKit                          0x2c4d9d63 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
27 UIKit                          0x2c4d9acd -[UINavigationController __viewWillLayoutSubviews] + 44
28 UIKit                          0x2c4d9a61 -[UILayoutContainerView layoutSubviews] + 184
29 UIKit                          0x2c42e24f -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
30 QuartzCore                     0x2be56a0d -[CALayer layoutSublayers] + 136
31 QuartzCore                     0x2be523e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360
32 QuartzCore                     0x2be5226d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
33 QuartzCore                     0x2be51c51 CA::Context::commit_transaction(CA::Transaction*) + 224
34 QuartzCore                     0x2be51a55 CA::Transaction::commit() + 324
35 UIKit                          0x2c4266e5 _afterCACommitHandler + 132
36 CoreFoundation                 0x28f34d95 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
37 CoreFoundation                 0x28f32453 __CFRunLoopDoObservers + 278
38 CoreFoundation                 0x28f3285b __CFRunLoopRun + 914
39 CoreFoundation                 0x28e803c1 CFRunLoopRunSpecific + 476
40 CoreFoundation                 0x28e801d3 CFRunLoopRunInMode + 106
41 GraphicsServices               0x302550a9 GSEventRunModal + 136
42 UIKit                          0x2c48ffa1 UIApplicationMain + 1440
43 AppName                        0x000eb500 main (AppDelegate.swift:21)
44 libdyld.dylib                  0x36fa6aaf start + 2

有人请帮忙找到我在这里缺少的东西。

1 个答案:

答案 0 :(得分:1)

看起来子类正在弄乱原始的初始化方法。 MKMapView的文档说你不应该是子类,而是将代理用于你需要的一切。

  

虽然您不应该对MKMapView类本身进行子类化,但您可以通过提供委托对象来获取有关地图视图行为的信息。

我建议您创建一个自定义UIView子类,其中包含MKMapView并充当其委托。这是我尝试解决此问题的第一步。