2017-10-13 4 views
0

私はビデオの背景のログインを再生するこのVCを持っています。 アウトレットとしての2つのボタンはストーリーボードからUIStoryboard Push Segueを直接起動します。ViewController deinitメソッドを呼び出さないswift 4

私が達成したいのは、ビデオのログインVCが、アプリケーションの通常のフローでこれに戻ってから、サイクルをさらに長くすることを避けるために、セグレスを通してVCのいずれかに移動した後です。

ありがとうございます。

@IBOutlet weak var loginButton: UIButton! 
@IBOutlet weak var registerButton: UIButton! 
weak var avPlayer: AVPlayer! 
weak var avPlayerLayer: AVPlayerLayer! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.loginButton = Util.roundBorderButton(button: self.loginButton, color: UIColor.white.cgColor, radius: 5, width: 1) 
    self.registerButton = Util.roundBorderButton(button: self.registerButton, color: UIColor.white.cgColor, radius: 5, width: 1) 

    if let resourceUrl = Bundle.main.url(forResource: "porsche-trimmed", withExtension: "mp4") { 
     if FileManager.default.fileExists(atPath: resourceUrl.path) { 

      avPlayer = AVPlayer(url: resourceUrl) 
      avPlayerLayer = AVPlayerLayer(player: avPlayer) 
      avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill 
      avPlayer.volume = 0 
      avPlayer.actionAtItemEnd = .none 

      avPlayerLayer.frame = view.layer.bounds 
      view.backgroundColor = .clear 
      view.layer.insertSublayer(avPlayerLayer, at: 0) 

      NotificationCenter.default.addObserver(self, 
                selector: #selector(playerItemDidReachEnd(notification:)), 
                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, 
                object: avPlayer.currentItem) 
     } 
    } 
} 

@objc func playerItemDidReachEnd(notification: Notification) { 
    let p: AVPlayerItem = notification.object as! AVPlayerItem 
    p.seek(to: kCMTimeZero, completionHandler: nil) 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    avPlayer.play() 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    NotificationCenter.default.removeObserver(self) 
} 

deinit { 
    print("deinit video") 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
+0

"さらに保持サイクルを避けるために" Que! deinitを呼び出すと、保持サイクルを避けることができますか? 「それ以上」と言えば、それはどこで起こりますか? –

答えて

0

通知センターにコントローラが残っていますか?通知サブスクリプションをコメントアウトし、問題が解決したかどうかを確認してください。表示されている場合は、退会するための適切な場所(viewWillDisappear?)を選択してください。

0

ケース1:

override func viewWillDisappear(_ animated: Bool)NotificationCenter.default.removeObserver(self)ラインを書いてみてください。 viewControllerのオブジェクトを保持する通知センターになることがあります。

ケース2: Segueを使用してコントローラからナビゲートする場合は、UIStoryboardSegueには、viewControllerのオブジェクトをソースviewControllerとして保持します。 この場合も最初のケースで問題を解決できます。

関連する問題