0

我在实施 3D Touch 快速操作时遇到问题。该操作应在地图视图上为用户的位置设置注释。当应用程序已经运行时,它工作得很好。但是,如果我杀死该应用程序并重试,它就会崩溃。

应用委托:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //shortcut Item


    let shortcut1 = UIMutableApplicationShortcutItem(type: "markLocation", localizedTitle: NSLocalizedString("Mark my location", comment: "Shortcut mark location"), localizedSubtitle: "", icon: UIApplicationShortcutIcon(type: .markLocation), userInfo: nil)



    application.shortcutItems = [shortcut1]


    return true
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    switch shortcutItem.type {
    case "markLocation":
        mapVC.addNewPin()
    default:
        print("")
    }


}

地图视图控制器(mapVC):

func addNewPin() {


    if lastAnnotation != nil {
        let alertController = UIAlertController(title: NSLocalizedString("Annotation already dropped", comment: "Error Message"), message: NSLocalizedString("There is an annotation on screen. Try dragging it if you want to change its location!", comment: "Eror Message"), preferredStyle: .alert)
        let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive) { alert in
            alertController.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(alertAction)
        present(alertController, animated: true, completion: nil)

    } else {
        let location = LocationAnnotation(coordinate: mapView.userLocation.coordinate, title: NSLocalizedString("To explore", comment: "Annotation Title"), subtitle: " ")

        mapView.addAnnotation(location)
        lastAnnotation = location
    }
}
4

0 回答 0