ios - 从 App Delegate 调用 ViewController 的方法

标签 ios swift appdelegate

在我的 ViewController、Inbox 中,我想从我的 App Delegate 调用方法 playAudio()。

这是方法。

func playAudio(){

        let nowPlaying = MPNowPlayingInfoCenter.defaultCenter()

        let albumArtWork = MPMediaItemArtwork(image: UIImage(named: "testImage.jpg")!)

        nowPlaying.nowPlayingInfo = [MPMediaItemPropertyTitle:"Sonnnngggg",
            MPMediaItemPropertyArtist:"Arrrtttiiiissstttt",
            MPMediaItemPropertyArtwork:albumArtWork]
        audioPlayer.play()

    }

我想从我的 App Delegate 中调用它...

if event!.subtype == UIEventSubtype.RemoteControlPlay {
                print("received remote play")
                //audioPlayer.play()
                Inbox.playAudio()
            }

一个问题是,因为我有一个audioPlayer 和我的Inbox ViewController 中的所有内容,并且它可能已经在播放音频,所以创建一个Inbox 实例来调用playAudio() 是没有意义的。因此,如果 Swift 中有任何解决方法,请告诉我。

谢谢, 利亚姆

最佳答案

在 AppDelegate 中更改为:

if event!.subtype == UIEventSubtype.RemoteControlPlay {
            print("received remote play")
            //audioPlayer.play()
            NSNotificationCenter.defaultCenter().postNotificationName("RemotePlayPressed", object: self)
        }

然后在收件箱 View Controller 的 viewDidLoad 添加:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playAudio", name: "RemotePlayPressed", object: nil)

同时补充:

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

到收件箱 View Controller 。

关于ios - 从 App Delegate 调用 ViewController 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711585/

相关文章:

iphone - 任何 iOS block 的标识

iphone - viewWillAppear/viewDidAppear 多次触发

ios - 如何将我的 UILabel 与系统时间同步?

ios - 重新呈现 ViewController 不会重新初始化它

ios - 定义一个不会被取消引用的委托(delegate)?

swift - 如何为导航栏 AppDelegate Swift 设置图像

ios - CKQueryOperation iOS 9仅可插入工作

ios - 在 Swift 中更新 Parse currentUser() 的缓存副本

ios - 根据firebase逻辑,在uicollectionview中快速分段控制显示/隐藏单元格

ios - 删除 applicationWillTerminate 中的 20000 个文件