我正在将FrontViewController
s视图加载到我当前的视图控制器MyVideosViewController
中。
我已经向我的MyVideosViewController
viewdidload()添加了2个通知观察者
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
但是当我点击手势时,这些方法中的任何一个都没有触发。这是什么原因?请帮我。
UPDATE
就像这样。如果我重新加载这个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)
你需要像这个(_:)
一样传递发件人,然后它会调用这个方法。
希望这对你有所帮助。