-1

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 { 
    do-some-other-things
} else {
    Start-DetailViewController-Normally
}
Michel
  • 8,287
  • 14
  • 60
  • 126
  • 2
    Just do that. What's the problem? – matt Jul 16 '16 at 05:06
  • You have to start by trying it yourself and post a question here when you are having problems with something. – Vladimir Nul Jul 16 '16 at 05:28
  • "Just do that" : What is "that"? To "Vladimir Nul" : Thank you for thinking on you own that I did not try anything by myself. If I take the time to write a post this is precisely because I tried several things and it did not work. As you write "because I am having problems". – Michel Jul 16 '16 at 05:38

2 Answers2

1

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
}
vacawama
  • 133,454
  • 26
  • 238
  • 261
  • Thanks a lot, it works. The interesting part is, I found a different solution on my own also working, through the overriding of another method: `func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath?`. I return nil when situation-is-not-good and indexPath otherwise. – Michel Jul 17 '16 at 23:58
  • Interesting. You can post answers to your own question. That information could help future programmers that find this question. – vacawama Jul 18 '16 at 00:03
  • Yes, I sometime do it. In this particular case your solution is as simple and as clear as mine. I don't know if one could be considered as best practice. – Michel Jul 18 '16 at 00:23
  • I just added my solution too. – Michel Jul 18 '16 at 00:30
1

Here is one possible solution:

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    let theCell = self.tableView.cellForRowAtIndexPath(indexPath)
    if situation-is-not-good for theCell {
        // Do-Whatever-Is-Needed
        return nil
    } else {
        return indexPath
    }
}
Michel
  • 8,287
  • 14
  • 60
  • 126