在打开Optional值时,SWRevealViewController意外地发现了nil

时间:2016-06-16 19:05:39

标签: ios swift swrevealviewcontroller

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

     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)

单击确定后,我在第二个ViewController中收到错误,我正在重定向

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())
        }
}

错误是致命错误:在展开“可选”值时意外发现nil

1 个答案:

答案 0 :(得分:2)

您可以尝试使用此代码来代替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

您可以在func

上添加viewController这样的内容
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())
                }
        }
  

被修改

您必须修改您提供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)

我希望这有助于你