0

Actually i want to move one view controller to another by using navigation controller and another is through seque by clicking to button accordingly. I have a two view controller, one is green view controller another is red view controller. A segue on Storyboard which start directly from a view controller named "toRedSegue". I have a 2 button in green view controller. When i click button 1 red view controller appear one time. If i click button 2 red view controller appear twice.

In another way i have added a segue on Storyboard which start directly from a Button it's same result. But if i removed the segue from storyboard it's works well.

My question is why same view controller appear twice?

Here is the code snippet

GreenVC.Swift
=============
/* This view controller contain two IBAction, one for button 1 
which implement the performSeque with seque identifier. Another 
for button 2 which implement the view transition by pushing view 
controller in directly on navigation controller using 
pushViewController API. */

// IBAction for button 1
@IBAction func button1PerformSegueAction(_ sender: Any) {
    performSegue(withIdentifier: "toRedSegue", sender: self) // move green to red view controller
}

// IBAction for button 2
@IBAction func button2PerformPushVCtoNavigationContronller(_ sender: Any) {
    let myVC = storyboard?.instantiateViewController(withIdentifier: "RedVC") as! RedVC
    myVC.value = "hello view transition through push navigation controller"
    navigationController?.pushViewController(myVC, animated: true) // push the view controller to the navigation stack.
}
/* As per my understanding this prepare method will call when view 
transition occur by seque. But i have not use any seque identifier
on button 2 click. */
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print("i am come from green view. now i am in red view")
    (segue.destination as! RedVC).value = "hello view transition through perform segue"
}    

RedVC.Swift
===========
var value : String?
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Red View"
    print("\(value!)")
}

Console output:
==============
i am come from green view. now i am in red view
hello view transition through push navigation controller
hello view transition through perform segue
Jahangir
  • 161
  • 2
  • 7
  • can u check this link https://stackoverflow.com/questions/46018202/same-view-controller-loads-twice – naga May 22 '19 at 10:19
  • Possible duplicate of [Same view controller loads twice](https://stackoverflow.com/questions/46018202/same-view-controller-loads-twice) – Ben Rockey May 22 '19 at 10:22
  • 1
    Check if you've connected the `segue` in `storyboard` as well. In that case the `segue` will execute twice - once from code and another from the `storyboard` – PGDev May 22 '19 at 10:34
  • I followed this post and tried. But as per my understanding my scenario is different. – Jahangir May 22 '19 at 10:34
  • If i remove seque from storyboard app will crash when i click button 2 (button2PerformPushVCtoNavigationContronller). error come with msg like "seque identified toRedSegue does not exist ". even though i had not click on button 1 – Jahangir May 22 '19 at 10:42
  • If you have created the segue between the two controllers and not from the UI button you should consider checking first the segue.identifier first. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { print("i am come from green view. now i am in red view") if segue.identifier == "toRedSegue" { (segue.destination as! RedVC).value = "hello view transition through perform segue" } } – Dimitrios Kalaitzidis May 22 '19 at 12:13
  • I have checked the seque identifier inside prepare override method same result with same console log i mentioned previous after click button 2 click destination view controller appear twice. I figured out one thing, if i remove performSegue(withIdentifier: "toRedSegue", sender: self) from @IBAction of button 1 it work properly. – Jahangir May 23 '19 at 04:38

0 Answers0