簡體   English   中英

Segue被叫了兩次,我不確定為什么-Swift

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

如果用戶尚未安裝應用程序,則我試圖在應用程序首次啟動時運行代碼塊。 當前,該代碼通過另一種方法(出於清潔目的)檢查存儲的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*

解決方案非常簡單。 我忘了在SetupViewController中創建一個viewDidAppear甚至viewDidLoad函數。 這樣就解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM