Same view controller loads twice
Asked Answered
S

3

4

I've read several posts on this issue but none of them solved my problem.

I'm coding an app where I have to click on a button ("Prepare") to go to the following ViewController. When the button is clicked, it also passes data between the two view controller.

The problem is, when I click the button, the following ViewController loads twice. Thus, if I want to go back I have to go back through two same ViewController.

I've checked the segue, the class names and files names but nothing fixes it.
I've also created a new project and rewritten all the code from the beginning but in the end, it still doesn't work.

However, I've noticed that the problem showed up when I added the prepare(forSegue:) function and the performSegue function. Without it the ViewController only loads once. But of course, I can't get the data passed between the views without it...

Here is the screenshot of my two view and the code of the two functions :

First view
Second view

//    Prepare the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "prepareSegue" {
        let timerViewController = segue.destination as! CountdownViewController
        timerViewController.timeInterval1 = waitingTime
        timerViewController.timeInterval2 = shootingTime
        timerViewController.lastSeconds = lastSeconds
        timerViewController.currentTimeInterval = waitingTime
    }
}

//    Prepare the timer when the button is pushed
@IBAction func prepareTimer(_ sender: Any) {
    performSegue(withIdentifier: "prepareSegue", sender: self)
}
Shutdown answered 2/9, 2017 at 21:6 Comment(2)
Do you have a segue set up in Interface Builder?Dagmardagna
In my case it was commentTF.becomeFirstResponder(), I just had to remove it.Pizzicato
C
15

Probably, when two ViewControllers appear it's because you have:

  1. A segue on Storyboard which start directly from a Button
  2. IBAction attached to the button where you call a performSegue of the same Segue

To fix this problem, you need to create a Segue which start directly from ViewController. After you can use the IBAction and the performSegue

Collete answered 2/9, 2017 at 21:19 Comment(4)
Indeed, I deleted the segue starting from the button and replaced it by one starting directly from the view controller, and it works ! Thank you very much Giuseppe Sapienza and @Christian ! I wasn't aware of this subtlety and You've just taught me something :)Shutdown
You are welcome! Use this approach ever and you will never be wrong ;)Collete
@GiuseppeSapienza How do you create "A Segue which starts directly from ViewController". It seems that it needs to start from an element.Economically
@Economically On the storyboard in Xcode, look closely at the top of your View Controller scenes. There should be a row of small icons inside a gray bar at the top of each one. The first of these is a circle with a square inside, and if you hover over it it has the tooltip "View Controller". Ctrl-click and drag starting from this and ending anywhere on the main background view of the view controller you want to perform the segue to. It should open a menu that lets you choose the type of segue.Crooked
R
2

You have added a seague but also an IBAction. If the seague is defined well in InterfaceBuilder it will perform and call your method. The IBAction is the alternative way for connecting an Action to a button. If you use both, you have two actions.

Roseboro answered 2/9, 2017 at 21:13 Comment(0)
M
0

Without the rest of your project to check, one thing it could be is a completion closure which contains the performSegue(...) statement is called twice, so the performSegue statement is literally run twice. How might this happen?

If you have a networking call inside the closure which contains performSegue command, and the networking call has a closure which calls completion(...) twice, then you could get either the segue occuring twice or even recursively!

It's rare this will be the case, or in your instance, but this type of problem can be uncovered by using lots of breakpoints and following the "bouncing ball" to see the completion() calls occuring more than once.

Morrissey answered 12/8, 2022 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.