Home > Software design >  When I use pushViewController I don't get any response
When I use pushViewController I don't get any response

Time:02-14

I try to play a video and I want to switch to a new storyboard afterwards. But for some reason the pushViewController is not doing anything.

I will appreciate any hint to get this running. Thank you all.

View Controller File

class ViewController: UIViewController, WKNavigationDelegate {
       
    let playerController = AVPlayerViewController()

    
    private func playVideo() {
        guard let path = Bundle.main.path(forResource: "screen3", ofType: "mp4" ) else {
            debugPrint("video missing")
            return
        }
        
        let player = AVPlayer(url: URL(fileURLWithPath: path) )
        
        playerController.showsPlaybackControls = false
        playerController.player = player
        playerController.videoGravity = .resizeAspectFill
        
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerController.player?.currentItem )
        
        present(playerController, animated: true ) {
            player.play()
        }
        
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification ) {
        print("Video finished")
        
        let vc = UIStoryboard.init(name: "Alternate", bundle: Bundle.main).instantiateViewController(withIdentifier: "SecondVC") as? SecondVC
                
        self.navigationController?.pushViewController(vc!, animated: false)
    }
  
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        playVideo()
 
    }
     
}

SecondVC

    class SecondVC: UIViewController {
    
    
    override func viewDidLoad() {
        
        print("alterante")
        super.viewDidLoad()
        view.backgroundColor = .link
    }
       
    
}

CodePudding user response:

I believe this is what is happening:

  1. You have a Navigation Controller which contains UIViewController
  2. You present the AVPlayerViewController on top of the UIViewController and its UINavigationController
  3. When you push the SecondVC, it is over UIViewController and its UINavigationController but under AVPlayerViewController so you don't see it

Why don't you update your playerDidFinishPlaying function to something like this:

@objc
func playerDidFinishPlaying(note: NSNotification ) {
    print("Video finished")
    
    let vc = UIStoryboard.init(name: "Alternate",
                               bundle: Bundle.main)
        .instantiateViewController(withIdentifier: "SecondVC") as? SecondVC
    
    // Push SecondVC
    self.navigationController?.pushViewController(vc!, animated: false)
    
    // Dismiss AVPlayerViewController
    dismiss(animated: true) {
        // do what you want
    }
}

Does this give you the results you were looking for ?

  • Related