我正在尝试向我的RevealViewController()
添加一个轻击和摇动手势识别器,并且得到一个Value of optional type 'UIGestureRecognizer?' must be unwrapped
。我应该如何拆开包装?
//我的应用
// Main.storyboard
// HomeVC
override func viewDidLoad() {
super.viewDidLoad()
menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
if self.revealViewController() != nil{
self.view.addGestureRecognizer(self.revealViewController()!.tapGestureRecognizer())
self.view.addGestureRecognizer(self.revealViewController()!.panGestureRecognizer())
}
}
这个问题含糊不清,您没有提供太多的背景信息,因此,如果这不是您真正需要的答案,请原谅,但为什么不做一个简单的if let
语句来优雅地解开包装呢。
if let gestureTap = self.revealViewController()?.tapGestureRecognizer(){
if let gesturePan = self.revealViewController()?.panGestureRecognizer(){
self.view.addGestureRecognizer(gestureTap)
self.view.addGestureRecognizer(gesturePan)
}
}
我通过添加:获得了点击和平移手势识别器:
menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.panGestureRecognizer), for: .touchUpInside)
menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.tapGestureRecognizer), for: .touchUpInside)