0

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

当我运行这个快捷方式时,我的应用程序崩溃了,因为 AppDelegate.swift 中的函数调用了我的根视图控制器中的一个函数,该函数实例化了 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与此错误一致:

线程 1:致命错误:在展开可选值时意外发现 nil

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

如何使它正常工作?

4

1 回答 1

0

I found the solution due to this post 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()
                }
            }
        }
    }
于 2017-11-14T20:05:48.243 回答