文件播放后我的应用程序崩溃(Swift)

时间:2015-05-19 02:10:05

标签: ios iphone swift

文件播放后,应用程序崩溃,我收到此错误:

  

- [fefef.ViewController lavEnFunktionDerSkifterTitle(sender as!UIButton)]:无法识别的选择器发送到实例0x7f9883785f40

我该如何解决?

这是我的代码:

@IBAction func play(sender: AnyObject) {

    musikAfspiller("wwww.myurl.com")
    lavEnFunktionDerSkifterTitle(sender as! UIButton)
}

func musikAfspiller (url: String) {

    let playerItem = AVPlayerItem( URL:NSURL( string:url ) )

    player = AVPlayer(playerItem:playerItem)

    player.play()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"lavEnFunktionDerSkifterTitle()",name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
}

func lavEnFunktionDerSkifterTitle(sender: UIButton) {

    if counter == 1 {

        counter = 2

        sender.setTitle("Stop", forState: UIControlState.Normal)

    } else {

        counter = 1
        sender.setTitle("Play", forState: UIControlState.Normal)

    }
}

2 个答案:

答案 0 :(得分:2)

观察者声明的语法应为:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "lavEnFunktionDerSkifterTitle:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)

并且接收函数应该在此语法的viewDidLoad()函数之外:

func lavEnFunktionDerSkifterTitle(notification: NSNotification) {}

答案 1 :(得分:1)

当你发送通知时,选择器声明不应该有括号:

 NSNotificationCenter.defaultCenter().addObserver(self, selector:"lavEnFunktionDerSkifterTitle",name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)

接收通知的函数的signeture应如下所示:

func lavEnFunktionDerSkifterTitle(notification: NSNotification) 

有关NSNotification的更多详细信息,请访问here