简体   繁体   English

Segue被叫了两次,我不确定为什么-Swift

[英]Segue is being called twice and I'm not sure why - Swift

I'm trying to run a block of code when the application first starts if the user hasn't setup the application yet. 如果用户尚未安装应用程序,则我试图在应用程序首次启动时运行代码块。 Currently, the code checks stored UserDefaults via another method (for cleanliness) to see if the application has been setup. 当前,该代码通过另一种方法(出于清洁目的)检查存储的UserDefaults,以查看是否已安装该应用程序。 That shouldn't matter in this case however, as I hard coded the software to think that the application has not yet been setup. 但是,在这种情况下这无关紧要,因为我对软件进行了硬编码,以为尚未安装该应用程序。 I then have the application check to see if it has been setup (referencing the hard code) within the viewDidAppear function for the ViewController. 然后,我让应用程序检查以查看是否已在ViewController的viewDidAppear函数中设置了该程序(参考硬代码)。 Upon seeing that it hasn't been setup, it then calls a method that asks to segue to a SetupViewController. 看到尚未设置后,它便调用一个方法,要求将其隔离到SetupViewController。 For some reason though, it is calling the segue twice. 但是由于某种原因,它两次调用segue。 So once the segue is performed and I see the other view, it seems to try and run it again and then throws an exception. 因此,一旦执行了segue并看到了另一个视图,它似乎会尝试再次运行它,然后引发异常。 Below is my code and my log's output. 以下是我的代码和日志的输出。 What in my code is incorrect, or what am I missing in the logic of my code? 我的代码中什么是不正确的,或者我的代码逻辑中缺少什么? I also checked and made sure the segue does have the proper identifier in the Main.storyboard. 我还检查并确保segue在Main.storyboard中确实具有正确的标识符。 Thanks in advance guys! 在此先感谢大家!

ViewDidAppear code: 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()
    }
}

Method calling for segue called "RunSetup:" 调用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")

}

Log Output: 日志输出:

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*

Solution came in a very simply way. 解决方案非常简单。 I forgot to create a viewDidAppear or even viewDidLoad function in the SetupViewController. 我忘了在SetupViewController中创建一个viewDidAppear甚至viewDidLoad函数。 This solved the problem. 这样就解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM