user1555112
user1555112

Reputation: 1987

ios swift: @IBAction handling to avoid segue

I have 2 views, first to enter some data and second to fill in more details based on the data the user entered on the first view.

Therfore I have a function like:

@IBAction func addNewCardSet(sender: AnyObject) { ...
let givenCardSetName = newCardSetName.text
if givenCardSetName != "" {
    ... save routine ...
}else{           
    updateLabel("Please fill in a name")
}

I also added a segue to the addNewCardSet Button to do a segue to the second view. What happens now is that if the user doesn't enter a name, I can see the message label saying "Please fill in the name" but one little moment later the segue takes place and send the user to the next view without any saved data...

What can I do to "allow" the segue only, if my save method took place with no errors and it is the time to do the segue?

Upvotes: 0

Views: 79

Answers (1)

Greg
Greg

Reputation: 25459

You can implement shouldPerformSegueWithIdentifier method:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
  if let ident = identifier {
    if ident == "YOUR IDENTIFIER HERE" {
        let givenCardSetName = newCardSetName.text
        if givenCardSetName != "" {
            return true
        }else{           
            return false
        }
      }
  }
  return true
}

Upvotes: 2

Related Questions