Michel Michel - 1 year ago 71
iOS Question

Master-Detail flow control

I started a Master-Detail iOS type of project in Xcode.

I have the MasterViewController and DetailViewController working as I expect.

Here is what I want to know how to do, using a good practice.

The usual behaviour is that when tapping on an item in the Master table-view, the DetailViewController fires up and does its job.

But there are cases when things are not ready and I do not want the DetailViewController to show up.
I just do not want anything to happen, or I want something else to happen. How can I do that? And what is the best (standard) way to do it?

In pseudo code I would like something like:

if situation-is-not-good {
} else {

Answer Source

Since you started with the Master-Detail template, you are using a segue with identifier "showDetail" to transition to the Detail View Controller. iOS provides a hook for you to insert decision making into whether that segue should be performed when the row is selected.

Override shouldPerformSegueWithIdentifier(_:sender:) and put your logic in there. Return true if you want the segue to proceed or false if you want to skip the segue.

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    if identifier == "showDetail" {
        if situation-is-not-good { 
            // do-some-other-things

            // if you don't let the segue proceed, then the cell remains
            // selected, so you have to turn off the selection yourself
            if let cell = sender as? UITableViewCell {
                cell.selected = false

            return false  // tell iOS not to perform the segue

    return true  // tell iOS to perform the segue