ios - SWRevealViewController 在解包 Optional 值时意外发现 nil

标签 ios swift swrevealviewcontroller

你好,在用户单击 UIAlertView 上的 ok 操作按钮后,我将用户重定向到其他 Controller 。

     let successAlert = UIAlertController(title: "Success", message: Message.TRIPISLIVE, preferredStyle: UIAlertControllerStyle.Alert)

            let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
           let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("MyTripsTableViewController")
  self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
            }
 successAlert.addAction(action)
 self.presentViewController(successAlert, animated: true, completion: nil)

单击确定后,我在重定向的 secondViewController 中出现错误

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);


         self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
        if revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = "revealToggle:"
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
}

错误是 fatal error :在展开可选值时意外发现 nil

最佳答案

你能试试这个而不是你的 viewWillAppear

代码吗
   override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated);

            if revealViewController() != nil {
                self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
                menuButton.target = self.revealViewController()
                menuButton.action = "revealToggle:"
                self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
            }
    }

EDITED

你可以在你的viewController上添加一个func

func customLeftButtonAction() {
        self.revealViewController().revealToggleAnimated(true);
    }

然后修改

override func viewWillAppear(animated: Bool) {
                super.viewWillAppear(animated);

                if revealViewController() != nil {
                    self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
                    menuButton.target = self
                    menuButton.action = #selector(self.customLeftButtonAction)
                    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
                }
        }

Edited

您必须修改您呈现viewControllerYouWantToPresent 的表单,所以将此代码改为

let successAlert = UIAlertController(title: "Success", message: Message.TRIPISLIVE, preferredStyle: UIAlertControllerStyle.Alert)

            let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
           let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("MyTripsTableViewController")
    self.revealViewController().setFrontViewController(viewControllerYouWantToPresent!, animated: false);
    self.revealViewController().revealToggleAnimated(true);
            }
 successAlert.addAction(action)
 self.presentViewController(successAlert, animated: true, completion: nil)

希望对你有帮助

关于ios - SWRevealViewController 在解包 Optional 值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37867468/

相关文章:

ios - UISegmentedControl 操作中的 UIButton

android - AS3 : Drawing Lines With Touch Screens

ios - swift 中的日期转换和日期差异问题

ios - 将 indexpath.row 转移到新的 View Controller ?

swift - Xcode 11 的 SWRevealViewController 问题

ios - SWRevealViewController - 切换到另一个时如何保留前 View

swift - SWReveal - CoreAnimation 警告 : stiffness/damping must be greater than 0

ios - 无法使用 tipe (UINT64, UINT64) 的参数列表调用 'dispatch_time'

Swift - 如何在菜单栏应用程序中跨窗口共享类(作为单例)?

arrays - 修剪数组的有效方法