gpt4 book ai didi

swift - PerformActionFor快捷方式AppDelegate.swift(Swift 4)

转载 作者:行者123 更新时间:2023-11-30 12:00:55 34 4
gpt4 key购买 nike

我想在我的应用程序上创建一个 3D Touch 快捷方式,我已经完成了有关快捷方式本身的所有操作,它显示正确,带有文本和图标。

当我运行这个快捷方式时,我的应用程序崩溃了,因为 AppDelegate.swift 中的函数调用我的根 viewController 中的一个函数,该函数实例化 AVPlayer(),使其播放,最后通过更改我的播放图像来更新用户界面/停止按钮,这就是我遇到问题的地方。

我给你下面的代码。

这是我的 AppDelegate.swift 的一部分:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
if shortcutItem.type == "fr.xxxxxxxxxxx.xxxxxxxxxxxxxx.playRadio" {
let playerVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"Player") as! ViewController
playerVC.playPlayer()
}
}

这是我的 ViewController.swift 的一部分:

@objc func playPlayer() {
initAVAudioSession()
setupPlayer()
RadioPlayer.rate = 1.0
RadioPlayer.play()
playButton.setImage(UIImage(named: "stopbutton"), for: .normal)
}

它在 playButton 行崩溃并出现以下错误:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

注意:如果我在 playPlayer() 内的 playButton 上插入问号 (?)(就像这样 playButton?.setImage(...) ),一切正常,但我的用户界面没有更新。我的 rootViewController 是放置在 Player:ViewController 之前的 UITabBarController

如何使其正常工作?

最佳答案

我通过这篇文章找到了解决方案this post

 func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
if shortcutItem.type == "fr.xxxxxxxxxx.xxxxxxxxxxxx.playRadio" {
//window!.rootViewController?.present("Player", animated: true, completion: nil)
//selectedViewController .playPlayer()
if let rootViewController = window?.rootViewController as? UITabBarController {
if let viewController = rootViewController.viewControllers?.first as? ViewController {
viewController.playPlayer()
}
}
}
}

关于swift - PerformActionFor快捷方式AppDelegate.swift(Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47247530/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com