问题描述
背景我的应用程序有一个带有 UIButton 的 MainViewController,按下时会导致另一个 ViewController.在第二个 ViewController 中是一个 UIBarButtonItem,它有一个到 MainViewController 的Back"segue.一切正常,直到现在我在 SWRevealViewController 的帮助下编写了一个滑出式菜单,我遵循了这个 Jared Davidson 教程 https://www.youtube.com/watch?v=8EFfPT3UeWs(简单、通用且容易)
BackgroundMy app has one MainViewController with a UIButton which leads to another ViewController when pressed. In the second ViewController is a UIBarButtonItem which has a "Back" segue to the MainViewController. Everything has worked fine, until now when I programmed a slide out menu with help from SWRevealViewController and I followed this Jared Davidson tutorial https://www.youtube.com/watch?v=8EFfPT3UeWs (simple, general and easy)
问题滑出菜单完美运行,但现在当按下第二个 ViewController 中的返回"按钮时,应用程序会因
ProblemThe slide out menu works perfectly, but now when the "Back" button in the second ViewController is pressed crashes the app due to
EXC_BAD_INSTRUCTION(代码=EXC_I1386_INVOP,子代码=0x0)
来自 MainViewController 文件中的以下代码,关于滑出菜单的平移手势.
from the following code in the MainViewController file, regarding the pan gesture for the slide out menu.
代码
这是我关于滑出菜单的 MainViewControllers 代码.它工作得很好,但它会在第二个 ViewController 上使用简单的返回"segue 进行推断.
This is my MainViewControllers code regarding my slide out menu. It works perfectly, but it inferrers with the simple "Back" segue on the second ViewController.
@IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) //THIS IS THE CODE WHERE THE ERROR ALEERT OCCURS
}
当最后一行代码被删除时,返回"转场再次起作用,但显然不是滑出菜单.
When the last line of code is deleted works the "Back" segue again, but obviously not the slide out menu.
帮助是否可以仅将平移手势代码指定给 MainViewController 及其滑出菜单,并让Back"segue 像以前一样只显示 MainViewController 和/或忽略"这行代码.或者是否有可能以其他方式将这两者分开并避免我的应用程序在按下第二个 ViewController(回到 MainViewController)的返回"segue 时崩溃?
HelpIs it possible specify the pan gesture code to only the MainViewController and its slide out menu, and let the "Back" segue only show the MainViewController like before and/or "ignore" this line of code.Or is it possible in some other way to separate this two and avoid my app from crashing when the "Back" segue from the second ViewController (back to the MainViewController) is pressed?
提前感谢传说.
推荐答案
首先你需要检查 Open
是否为零.当 UIViewController
未正确创建时,可能会发生此错误.试试这个代码:
First you need to check Open
is nil. This error may occur when UIViewController
was created not properly. Try this code:
@IBOutlet weak var Open: UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
if Open == nil {
assertionFailure("Open is nil")
}
if let revealViewController = revealViewController() {
Open?.target = revealViewController
Open?.action = "revealToggle:"
view.addGestureRecognizer(revealViewController.panGestureRecognizer())
}
}
这篇关于带有 SWRevealViewController 的 Exc_bad_instruction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!