我已在MKMapView
上创建了大小为200x200的自定义UIViewController
,以显示从当前位置到目标位置的路径。它显示位置和路径。但现在我想解雇这个MKMapView
,我希望看到我的UIViewController
。
所以我在MKMapView
的右上角创建了一个按钮,但我无法移除MKMapView
。
我在按钮的操作上尝试了下面这段代码。
func close() {
mapView!.removeFromSuperview()
}
我已经声明了这样的地图视图:
var mapView:MKMapView? = MKMapView()
然后我从viewDidLoad()中调用了一个方法showMap(),如下所示:
func showMap() {
//Setup our Map View
mapView!.mapType = .Standard
mapView!.showsUserLocation = true
mapView!.frame = CGRectMake(topView.frame.origin.x, topView.frame.origin.y, topView.frame.size.width, topView.frame.size.height)
mapView!.delegate = self
view.addSubview(mapView!)
}
在此之后,我这样调用了showCloseButtonForMap():
func showCloseButtonForMap() {
let mapCrossBtn = UIButton()
mapCrossBtn.frame = CGRectMake(crossBtn.frame.origin.x+20, crossBtn.frame.origin.y+20, crossBtn.frame.size.width, crossBtn.frame.size.height)
if let image = UIImage(named: "X@2x.png") {
mapCrossBtn.setImage(image , forState: .Normal)
}
mapCrossBtn.backgroundColor = UIColor.blackColor()
mapCrossBtn.addTarget(topView, action: "close", forControlEvents: .TouchUpInside)
view.addSubview(mapCrossBtn)
}
这里crossBtn是另一个现在隐藏的按钮。 任何帮助将不胜感激。
错误日志: 通讯错误:{count = 1,contents = " XPCErrorDescription" => {length = 22,contents ="连接中断" } }>
2015-11-19 15:02:27.390 YEWADemo [2328:235956] - [UIView close]:无法识别的选择器发送到实例0x7fc6d0c32270 2015-11-19 15:02:27.408 YEWADemo [2328:235956] *由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [UIView关闭]:无法识别的选择器已发送例如0x7fc6d0c32270' * 第一次抛出调用堆栈: ( 0 CoreFoundation 0x0000000107175f45 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000108e9cdeb objc_exception_throw + 48 2 CoreFoundation 0x000000010717e56d - [NSObject(NSObject)doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00000001070cbeea ___ forwarding _ + 970 4 CoreFoundation 0x00000001070cba98 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000107995e91 - [UIApplication sendAction:to:from:forEvent:] + 92 6 UIKit 0x0000000107b014d8 - [UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x0000000107b017a4 - [UIControl _sendActionsForEvents:withEvent:] + 311 8 UIKit 0x0000000107b008d4 - [UIControl touchesEnded:withEvent:] + 601 9 UIKit 0x0000000107a03ed1 - [UIWindow _sendTouchesForEvent:] + 835 10 UIKit 0x0000000107a04c06 - [UIWindow sendEvent:] + 865 11 UIKit 0x00000001079b42fa - [UIApplication sendEvent:] + 263 12 UIKit 0x000000010798eabf _UIApplicationHandleEventQueue + 6844 13 CoreFoundation 0x00000001070a2011 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 14 CoreFoundation 0x0000000107097f3c __CFRunLoopDoSources0 + 556 15 CoreFoundation 0x00000001070973f3 __CFRunLoopRun + 867 16 CoreFoundation 0x0000000107096e08 CFRunLoopRunSpecific + 488 17 GraphicsServices 0x000000010b993ad2 GSEventRunModal + 161 18 UIKit 0x000000010799430d UIApplicationMain + 171 19 YEWADemo 0x0000000106c2cdfd main + 109 20 libdyld.dylib 0x000000010c27a92d start + 1 21 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止
答案 0 :(得分:1)
您的mapCrossBtn
目标应为self
(通常是当前视图控制器),topView
没有“close
”功能。
尝试改变:
mapCrossBtn.addTarget(topView, action: "close", forControlEvents: .TouchUpInside)
到
mapCrossBtn.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)