0

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.)?

Community
  • 1
  • 1
hoffware
  • 205
  • 6
  • 18

2 Answers2

0

If you want to the action to perform before the segue, create and if-statement that determines that the action was completed successfully, then perform segue. ie.

if (ActionSuccess) {
self.performSegueWithIdentifier("Segue", sender: nil)
}
JCDOSAJ
  • 89
  • 1
  • 4
  • Yes, that would be needed, sorry for not clarifying that. How can I get the segue to work without attaching the button to the exit on the view controller, because by attaching it, it performs the prepareForSegue and unwind before performing the button action? – hoffware Apr 10 '15 at 02:30
0

Remove the exit segue from the button and instead simply link the button to a new IBAction and insert

self.dismissViewControllerAnimated(true, completion: nil)

So, your new code would be:

@IBAction func saveFriendRequest(sender: AnyObject) {
   //Do your stuff to save the friend request
   self.dismissViewControllerAnimated(true, completion: nil)
}
Shades
  • 5,336
  • 6
  • 26
  • 44