我正在将FrontViewController视图加载到当前视图控制器MyVideosViewController中。
我在我的MyVideosViewControllerviewdidload()中添加了两个通知观察员

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playLatestvideoClick), name: latestKey, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playMostWatchedvideoClick), name: mostKey, object: nil)

在我的FrontViewController
 //MARK: -Gesture Play
func playLatestvideoClick(sender: UITapGestureRecognizer)
{
   print("----- PLAY GESTURE TAPPED ------")
    let imgVw = sender.view as! UIImageView
    api.selectedVideoID = String(imgVw.tag)
    let homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("MYVIDEO") as? MyVideosViewController

    NSNotificationCenter.defaultCenter().postNotificationName(self.latestKey, object: nil)
}

func playMostWatchedvideoClick(sender: UITapGestureRecognizer)
{
    print("----- PLAY GESTURE TAPPED ------")
    let imgVw = sender.view as! UIImageView
    api.selectedVideoID = String(imgVw.tag)
    let homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("MYVIDEO") as? MyVideosViewController

    NSNotificationCenter.defaultCenter().postNotificationName(self.mostKey, object: nil)
}

这就是我如何在FrontViewController中设置手势操作的方法
let tapPlay = UITapGestureRecognizer.init(target: self, action: (#selector(FrontViewController.playLatestvideoClick(_:))))
        self.imgVideo.addGestureRecognizer(tapPlay)
        self.imgVideo.userInteractionEnabled=true

但当我敲击手势时,这些方法中的任何一种都不会开火。这是什么原因?
请帮帮我。
更新
就这样。如果我重新加载这个MyVideoViewController,它将第一次工作。那就没用了

最佳答案

请尝试以下代码:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playLatestvideoClick(_:)), name: latestKey, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playMostWatchedvideoClick(_:)), name: mostKey, object: nil)

您需要像这样传递发送者,然后它将调用此方法。
希望这对你有帮助。

10-08 00:47