2

I don't get what I am doing wrong, I am trying to perform a push segue by an identifier so that I can move to other view-controller when an IF condition gets TRUE

the segue is always performed either I add this method : override func prepareForSegue()

     @IBAction func btnClicked(sender: AnyObject) {
    if(n.isEqualToString("mpc01") && p.isEqualToString("mpc01"))
               {
    self.performSegueWithIdentifier("login_to_dashboard", sender: nil)
               }
               else{
                println("no data retrieved")
                }
    }
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "login_to_dashboard" {
           // let secondViewController = segue.destinationViewController as VC_dashboard
            println("performed segue")
        }
        else{
        println("recieved other command")
        }
    }

my ViewController storyboard ID : SID_login

my SecondViewController storyboard ID : SID_dashboard

my Push Segue ID : login_to_dashboard

I want to perform segue by its ID only , not directly

Ziad Tareq
  • 41
  • 6

6 Answers6

0

You don't have the control when you use - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. Instead, you can have control to trigger the segue or not by using this: - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

The prepareForSegue just let you doing some custom works before triggering.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • I replaced 'performSegueWithIdentifier()' with 'shouldPerformSegueWithIdentifier()' but still result is **same ** . It just jumps to second view controller without concerning the IF-ELSE . I want to stop it going that way – Ziad Tareq Jan 17 '15 at 09:40
0

In prepareForSegue method you are checking if segue.identifier != "login_to_dashboard", I think you need to change it to == to perform segue.

@IBAction func btnClicked(sender: AnyObject) {
        if(n.isEqualToString("mpc01") && p.isEqualToString("mpc01"))
        {
          self.performSegueWithIdentifier("login_to_dashboard", sender: nil)
        }
        else {
               println("no data retrieved")
        }
     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if (segue.identifier == "login_to_dashboard") {
                let secondViewController = segue.destinationViewController as SecondViewController
                println("performed segue")
            }
            else{
            println("recieved other command")
            }
        }
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • yes that was my mistake I admit , but I changed the condition , still no difference . – Ziad Tareq Jan 17 '15 at 10:11
  • I created a segue from a button to view controller , is that an issue ? – Ziad Tareq Jan 17 '15 at 10:17
  • what is VC_dashboard, if you have created segue from button then you don't need to do it through code – Suhit Patil Jan 17 '15 at 10:34
  • I you want to use code then create manual segue from ViewController to SecondViewController then call self.performSegueWithIdentifier in btnClicked method – Suhit Patil Jan 17 '15 at 10:35
  • http://stackoverflow.com/questions/26189588/move-to-another-viewcontroller I referred to this when I perform a segue – Ziad Tareq Jan 17 '15 at 11:04
  • I created a manual segue and removed the segue I did from button to other viewcontroller, the condition started working now , but still when condition is TRUE it does not go to other view controller – Ziad Tareq Jan 17 '15 at 11:07
  • is your first view controller embed in NavigationController if you are using push segue? – Suhit Patil Jan 17 '15 at 11:13
  • let secondViewController = segue.destinationViewController as SecondViewController is commented in your prepareForSegue method – Suhit Patil Jan 17 '15 at 11:22
  • yes you are right I am using embedded navigation controller for push segue – Ziad Tareq Jan 17 '15 at 11:34
0

You should use this method instead

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         if segue.identifier == "login_to_dashboard" {
              return true
         }
         else{
              return false
         }
}
Ali Riahipour
  • 524
  • 6
  • 20
0

this is very strange but since segue inherited from NSObject you can try:

    if(segue.valueForKey("_identifier") as String == "login_to_dashboard"){
        var targetController:VC_dashboard = segue.destinationViewController as VC_dashboard
        targetController.doSomthing()
    }
Fareed Alnamrouti
  • 30,771
  • 4
  • 85
  • 76
0

I did the mistake , solution to my question is to create a segue from view controller to other view controller rather a button to view-controller

Ziad Tareq
  • 41
  • 6
0

I am sorry I am actually a beginner to xcode Swift , the question I asked was totally different to my segue issue. I actually created a segue directly from the button insteads of the first responser , thats why it always resulted to segue to another view-controller

Ziad Tareq
  • 41
  • 6