removeFromSuperview不工作 - Swift

时间:2015-03-12 21:28:25

标签: ios xcode swift

请参阅以下代码。我正在尝试在按下“完成”按钮或视频停止播放时从视图中删除视频子视图。我在代码中没有显示错误,但removeFromSubview方法似乎没有工作。我不确定我的语法是否错误,或者是否与在IBAction方法中使用movieplayer代码以及在viewDidLoad下面的moviePlayBackDidFinish有关。任何建议非常感谢。谢谢

import Foundation
import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var moviePlayer:MPMoviePlayerController!

@IBAction func videoLaunch(sender: AnyObject) {
playVideo()
}
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("MyVideo", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
moviePlayer?.controlStyle = MPMovieControlStyle.Fullscreen
player.prepareToPlay()
self.view.addSubview(player.view)

}  
}


override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: "moviePlayBackDidFinish:",
    name: MPMoviePlayerPlaybackDidFinishNotification,
    object: moviePlayer)


func moviePlayBackDidFinish(notification: NSNotification){
    self.view.removeFromSuperview()
}



}
}

1 个答案:

答案 0 :(得分:1)

您正尝试删除self.view而不是moviePlayer.view

将您的moviePlayBackDidFinish代码更改为:

func moviePlayBackDidFinish(notification: NSNotification)
{
    if let player = moviePlayer
    {
      player.view.removeFromSuperview()
    }
}
相关问题