0

In my view controller, upon a specific if condition, I want to move automatically to the next view controller. I tried this code. But it won't work. Please help.

if rightCounter == 5 {

        var Level2 = self.storyboard?.instantiateViewControllerWithIdentifier("Level2") as? ViewController
        self.presentViewController(Level2!, animated: true, completion: nil)
    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    And what is the problem precisley? The code seems fine. Is the app crashing? Are there any logs in console? Have you tried debugging and checking if Level2 is not nil? – Losiowaty Aug 16 '15 at 00:03

2 Answers2

0
    if condition == true {
        //use either VC Name associate or the Storyboard restoration id
        //replace this with yours 
        if let Level2 = self.storyboard!.instantiateViewControllerWithIdentifier("MainVC") as? UIViewController
        {
            self.presentViewController(Level2, animated: true, completion: nil)
        }


    }

I think the problem is with you trying to cast the another VC to ViewController which i presume is the one that you are trying to segue from.

Make sure to have the storyboard ID and you can either type case to UIViewController to be broad or your specific VC to be specific.

enter image description here

Hope it helps.

kandelvijaya
  • 1,545
  • 12
  • 20
  • So any idea..how do we make a button appear only under certain condition ? Please help! Using an if statement itself! – Shaham Kampala Aug 16 '15 at 18:35
  • glad to hear that, it helped. You can certainly use .hidden property and set it as per your needs. But i would want some more clarification as what you like to accomplish to really point you in the right direction. – kandelvijaya Aug 17 '15 at 15:21
  • Can you please help me with this question @kandelvijaya : http://stackoverflow.com/questions/32150707/warning-attempt-to-present-guessmefinal-on-guessmefinal-viewcontroller-whos?noredirect=1#comment52212294_32150707 – Shaham Kampala Aug 23 '15 at 12:56
0

If you want automatic execution of the conditional statement posted, you could put the code in a property observer for rightCounter.

So where rightCounter is declared change its declaration to include a property observer.

Example could be something like-

var rightCounter: Int = 0 {
     didSet {
      if rightCounter == 5 {
            var Level2 = self.storyboard?.instantiateViewControllerWithIdentifier("Level2") as? ViewController
            self.presentViewController(Level2!, animated: true, completion: nil)
            }
      }
}

With the above code didSet will be called every time rightCounter is set, even if set to the same value as to what it currently is.

Bamsworld
  • 5,670
  • 2
  • 32
  • 37
  • Can you please help me with this question @Bamsworld : http://stackoverflow.com/questions/32150707/warning-attempt-to-present-guessmefinal-on-guessmefinal-viewcontroller-whos?noredirect=1#comment52212294_32150707 – Shaham Kampala Aug 23 '15 at 13:01