如何在RevealViewController()手势识别器上修复“可选nil”?

问题描述 投票:-1回答:2

我正在尝试向我的RevealViewController()添加一个轻击和摇动手势识别器,并且得到一个Value of optional type 'UIGestureRecognizer?' must be unwrapped。我应该如何拆开包装?

//我的应用

enter image description hereenter image description here

// Main.storyboard

enter image description here

// 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())
    }
}
swift xcode uigesturerecognizer swrevealviewcontroller
2个回答
0
投票

这个问题含糊不清,您没有提供太多的背景信息,因此,如果这不是您真正需要的答案,请原谅,但为什么不做一个简单的if let语句来优雅地解开包装呢。

if let gestureTap = self.revealViewController()?.tapGestureRecognizer(){
     if let gesturePan = self.revealViewController()?.panGestureRecognizer(){

self.view.addGestureRecognizer(gestureTap)
    self.view.addGestureRecognizer(gesturePan)
     }
}

0
投票

我通过添加:获得了点击和平移手势识别器:

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)
© www.soinside.com 2019 - 2024. All rights reserved.