如果用户尚未设置应用程序,我正在尝试在应用程序首次启动时运行一段代码。目前,代码通过另一种方法(清洁度)检查存储的UserDefaults,以查看是否已设置应用程序。然而,在这种情况下,这应该无关紧要,因为我硬编码软件认为应用程序尚未设置。然后,我检查应用程序是否已在ViewController的viewDidAppear函数中设置(引用硬代码)。在看到它尚未设置后,它会调用一个方法,要求转到SetupViewController。出于某种原因,它会两次调用segue。因此,一旦执行了segue并且我看到了另一个视图,它似乎尝试再次运行它然后抛出异常。下面是我的代码和我的日志输出。我的代码中有什么不正确,或者我的代码逻辑中缺少什么?我还检查并确保segue在Main.storyboard中有正确的标识符。先谢谢你们!
ViewDidAppear代码:
/*
ViewDidAppear - If the view has appeared after loading we will run some code to check the state of the app.
*/
override func viewDidAppear(_ animated: Bool) {
// If the application hasn't been setup and it believes it isn't setup run setup.
if(checkIfSetup() != true && hasSetup == false)
{
print("attempted run")
runSetup()
}
// If the application has been setup and believes it has been setup, load view.
else if(checkIfSetup() == true && hasSetup == true)
{
loadApp()
}
}
调用segue的方法称为“RunSetup:”
/*
RunSetup - Runs the setup for the application environment.
*/
func runSetup()
{
print("Running setup for AppName") // Notify the log that you are running the App's setup.
// ## Start by clearing all possibly stored data. ##
//REALM - Destroy Database if it exists
try! realm.write {
realm.deleteAll()
}
//Begin setting up the environment
self.performSegue(withIdentifier: "dismissAndCreate", sender: self)
print("tryingSegue")
}
日志输出:
attempted run
Running setup for AppName
tryingSegue
attempted run
Running setup for AppName
2017-04-03 17:12:57.404 AppName[22109:3226052] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<AppName.SetupAppViewController: 0x7ffc97824400>) has no segue with identifier 'dismissAndCreate''
*First Throw Stack is then listed*
答案 0 :(得分:1)
解决方案以一种非常简单的方式出现。我忘了在SetupViewController中创建一个viewDidAppear甚至viewDidLoad函数。这解决了这个问题。