Search code examples
iosswiftuitableviewdatasourceuisplitviewcontroller

Master/Detail - clear detail if object deleted or no objects left in Master (iOS)


In my iPad app (or iPhone 6 Plus in landscape) I am using a master/detail UISplitViewController. When the user deletes the currently selected item from the Master or deletes all the items from the Master, the previously selected item's detail remains in the Detail view.

What do I need to do to ensure that if they delete the currently selected item or the last item in the data source (this might not be necessary) that the Detail view is cleared back to its empty, default state?

Is there a way for this to happen automatically through UITableView/DataSource magic?

(Swift please if possible, but Objective-C is OK too.)


Solution

  • store a Item property in master view.

    var currentPresentingItem : <yourItemClass>?
    

    currentPresentingItem is not nil only when detail view is presenting it. So, in normal iphone, people will never have a chance delete a item in masterView when currentPresentingItem is not nil

    func segueToDefaultDetailViewIfPresentingItemDeleted(deletingItem : <yourItemClass>){
         if currentPresentingItem == deletingItem{
              // segue to your default detail view, 
              // this will never happen in normal iphone
         }
    }