I have created a segue (present modally) from view controller A to view controller B that is triggered through a button in the navigation controller of view controller A. The modal that takes over is used to send a friend request by typing in an email. When a user types in the email and presses a button to submit the friend request, I want to perform an action triggered by the button (calling a server to send the friend request and returning success if the email exists or returning error if the email doesn't exist). If success, I want to exit/unwind the segue back to A. If error, I don't want to exit/unwind the segue.
I have looked into this question, but it doesn't seem to have the implementation of what I need. I am trying the following:
class BViewController: UIViewController {
// The button function
@IBAction func sendFriendRequest(sender: AnyObject) {
println("Button Function.")
self.performSegueWithIdentifier("SendFriendRequest", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendFriendRequest" {
println("Preparing for segue.")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And then in first view controller:
AViewController: UIViewController {
// The unwind function
@IBAction func saveFriendRequest(segue:UIStoryboardSegue) {
println("Finished the segue.")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is the result when I set up the exit segue for the button (ctrl-drag the button to 'Exit' in the ViewController), I get the calls in this order:
Preparing for segue.
Finished the segue.
Button Function.
However, I don't necessarily want to call the prepareForSegue or the unwind function every time, only if the server gives a success message. So how would I connect things in the storyboard so that when I call performSegueWithIdentifier, I don't have to have call the other functions (prepareForSegue, etc.)?