2
votes

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

6
have you checked is your if condition is true?Suhit Patil
yes i have checked it,its TRUEZiad Tareq
I think you need to change the if condition in prepareforsegue method to if (segue.identifier == "login_to_dashboard") { //println("performed segue") }Suhit Patil
see the answer i postedSuhit Patil
create segue from view controller to other view controller rather a button to view-controller if you do so it wont check conditionvijeesh

6 Answers

0
votes

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.

0
votes

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")
            }
        }
0
votes

You should use this method instead

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         if segue.identifier == "login_to_dashboard" {
              return true
         }
         else{
              return false
         }
}
0
votes

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()
    }
0
votes

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

0
votes

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