'Hide the DetailsViewController of splitViewController in iOS

I have master view and some DetialsViewController.

When I present the detailsViewController I use this code:

  mainStoryboard = UIStoryboard(name: "StoryboardiPad", bundle: nil)
            let vc = mainStoryboard!.instantiateViewController(withIdentifier: "tableVC") as! tableVC
       
            vc.showingLoginInfo = true
            let navBarOnModal: UINavigationController = UINavigationController(rootViewController: vc)
            
            splitViewController?.preferredDisplayMode = .automatic
              splitViewController?.showDetailViewController(navBarOnModal, sender: nil)

but main problem how can I hide or dismiss the

DetialsViewController

after I used

showDetailViewController

I found this method but seems I need to initialise the DetialsViewController as secondaryViewController... IS THAT RIGHT?



Solution 1:[1]

You will need to create a second ViewController for your DetailView.

One way to create a split view is to delete the original ViewController on the main storyboard (select view and tap delete key) and drag a new Split View Controller onto the main storyboard from the Objects library. The split screen view controller contains a table view, details view and navigation controller.

This split view controller handles all the size configurations to view on iPhone or iPad without additional code. This means the view changes from split view in landscape to a single view when in portrait mode without extra work.

Now delete the default View Controller.swift file and create 2 new view controllers files (?N). These will be Cocoa Touch Class files. Name the first controller MasterTableViewController and give it a subclass of UITableViewController. Name the second DetailViewController with a subclass of UIViewController.

Next select the Split View Controller in the Main.storyboard. In the attributes inspector under the View Controller section, check the box for ‘is Initial View Controller’. There should now be an arrow on the left of the Split View Controller indicating that it is now the initial controller.

In main.storyboard, select the TableViewController and change the Class to MasterViewController in the Identity Inspector. Then select the DetailView controller and change its Class to DetailViewController in the Identity Inspector.

Goto MasterTableViewController.swift. Update the table view functions to display your data in the list view as desired.

To test without data: Update the numberOfSections func to return 1. Change the numberOfRowsInSection to return 10. This will be the count of items in your data set if you have data already (ex. return dataSet.count).
Uncomment the cellForRowAt func and change the withIdentifier to “Cell”. This will be your prototype cell’s identifier.

Back in Main.storyboard, select the prototype cell in the TableView controller. In the Attributes Inspector, set the Identifier to Cell to match the cellForRowAt func in the MasterTableViewController.swift.

Running the app now will present the split view with the list on the left and details on the right in landscape on iPad and larger iPhones. In portrait and smaller iPhones the app will open to the details page. Tapping the back button will hide the detail page and show your list.

From here you just need to finalize the detail view to present your data details as you would like.

For a detailed tutorial check out: https://www.raywenderlich.com/4613809-uisplitviewcontroller-tutorial-getting-started

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TripleMonkey