打开MapViewController并在用户点击通知横幅时居中显示经纬度。

huangapple go评论72阅读模式
英文:

Open MapViewController and center on Lat/Long when user taps notification banner

问题

我现在是你的中文翻译:

我遇到了一些困难。我是Swift的新手,但一直在进步。我有一个应用程序,在用户放置在地图上的位置时,用户会收到通知。我试图做的是,当用户点击通知时,启动MapViewController,然后将地图视图居中显示在通知中传递的纬度/经度上。

问题是,当MapViewController加载时,从故事板中的所有IBOutlet引用都为空,因此应用程序会崩溃。我该如何解决这个问题?(即使是chatGPT也碰到了难题 打开MapViewController并在用户点击通知横幅时居中显示经纬度。

我当前是在AppDelegate.swift文件中的'userNotificationCenter'函数的'didReceive'方法中调用MapViewController。以下是代码示例:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse) async {
    guard let window = UIApplication.shared.windows.first else { return }

    if let rootViewController = window.rootViewController as? UITabBarController {
        // 设置要切换到的所需选项卡索引
        let desiredTabIndex = 1

        // 选项卡索引是否在范围内
        guard desiredTabIndex < rootViewController.viewControllers?.count ?? 0 else { return }

        // 分配所需的VC
        let selectedViewController = rootViewController.viewControllers?[desiredTabIndex]

        // 检查所选的VC是否为MapViewController
        if let notificationViewController = selectedViewController as? MapViewController {
            // 创建地图VC的实例
            let notificationViewController = MapViewController()

            // 传递给目标视图控制器
            notificationViewController.notificationData = notificationData

            // 显示目标视图控制器
            rootViewController.present(notificationViewController, animated: true, completion: nil)
        }
    }
}

希望这对你有所帮助!如果有任何其他问题,请随时提出。

英文:

So I'm having a tough time. I'm new to Swift but have been getting better. I have an app where users will receive a notification when something is placed on the map. What I'm trying to do is launch the MapViewController when the user taps on the notification, then center the map view on the Lat/Long passed in the notification.

The issue is, when the MapViewController loads, all of it's IBOutlet references from the storyboard are nil so the app crashes. How do I pull this off? (even chatGPT is hitting a brick wall 打开MapViewController并在用户点击通知横幅时居中显示经纬度。

I'm currently calling the MapViewController from the AppDelegate.swift file in the the 'didRecieve' method of the 'userNotificationCenter' function. Here's what that looks like:

	func userNotificationCenter(_ center: UNUserNotificationCenter,
							didReceive response: UNNotificationResponse) async {

	guard let window = UIApplication.shared.windows.first else {return}
			
	if let rootViewController = window.rootViewController as? UITabBarController {
		// Set the desired tab index to switch to
		let desiredTabIndex = 1
		
		// is tab index in range
		guard desiredTabIndex &lt; rootViewController.viewControllers?.count ?? 0 else {return}
		
		// assign the desired VC
		let selectedViewController = rootViewController.viewControllers?[desiredTabIndex]
		
		// Check if the selected VC is the MapViewController
		if let notificationViewController = selectedViewController as? MapViewController {
			// Create an instance of the map VC
			let notificationViewController = MapViewController()
			
			// pass to the destination view controller
			notificationViewController.notificationData = notificationData
			
			// Present the destination view controller
			rootViewController.present(notificationViewController, animated: true, completion: nil)
		}
	}

}

答案1

得分: 0

第一部分(启动地图视图控制器)中,我意识到所有的视图控制器都由一个标签栏控制器控制。所以我只需要改变标签栏:

guard let window = UIApplication.shared.windows.first else {return}
		
if let tabController = window.rootViewController as? UITabBarController{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let myViewController = storyboard.instantiateViewController(withIdentifier: "MapViewController") as? MapViewController {
        tabController.selectedIndex = 1
    }
}

第二部分中,我使用了以下代码:

let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let mRegion = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: locSpan, longitudeDelta: locSpan))
self.mapView.setRegion(mRegion, animated: true)
英文:

For the first part (launching map viewcontroller), I realized all of my VC's were controlled by a tabbarcontroller. So all I had to do was change the tab:

guard let window = UIApplication.shared.windows.first else {return}
	
if let tabController = window.rootViewController as? UITabBarController{
	let storyboard = UIStoryboard(name: &quot;Main&quot;, bundle: nil)
	if let myViewController = storyboard.instantiateViewController(withIdentifier: &quot;MapViewController&quot;) as? MapViewController {
		tabController.selectedIndex = 1
	}
}

For the second part, I used the following:

let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let mRegion = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: locSpan, longitudeDelta: locSpan)
self.mapView.setRegion(mRegion, animated: true)

huangapple
  • 本文由 发表于 2023年6月22日 05:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定