I have a bit of a unique UISplitViewController
setup (I think). My master view controller is a table view controller within a navigation controller, which is normal. However, my detail view controller is a UITabBarController
. Each tab has a navigation controller to wrap the content within.
When the user selects a table row within the master view controller, I select a tab in the detail view controller and start pushing a view controller:
let detailViewController = masterViewController?.splitViewController
.viewControllers[1] as? UITabBarController
let selectIndex = 2
let viewController = (detailViewController.viewControllers?[selectIndex] as? UINavigationController)?.viewControllers.first
detailViewController.selectIndex = selectIndex
viewController.show(myCustomViewController)
This works great on iPad.. selecting an item in the master view controller table selects the tab in the detail view controller and pushes view controllers to it.
On iPhone, I have the master view controller shown on initial load using this post:
class MasterViewController: UIViewController {
private var collapseDetailViewController = true
override func viewDidLoad() {
super.viewDidLoad()
splitViewController?.delegate = self
}
// ...
}
extension MasterViewController: UISplitViewControllerDelegate {
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
return collapseDetailViewController
}
}
extension MasterViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
collapseDetailViewController = false
selectTabAndPush(index: 2, vc: myCustomViewController)
// Tried swapping the order of these but still no luck
}
}
However, selecting a row in the master view controller table literally does nothing. How can I get the detail view controller to show when a row in the master table is selected?
At very first, why you need both functionalities : Master-Detail
and Tab
. Because if you are implementing master detail with tab then you must have equal number of tab with the equal number of rows in Master
, then only you can navigate to the tab when you select row. In iPhone there would be max 5 tabs only and if you have 7 rows in Master
then what happened if user will click on row 6 and 7?
Because as per design concept : You should choose only one as per requirements.
Master-Detail
-> Provides facilities like side menu, so user can access many more option from these.TabBar
-> Provide facilities of more option with having menu at bottom.Anyway, you might have some unique requirement.
As you said on iPad it is working fine. So for iPhone you can go with below way :
UITabBarController
in your project. (I have created tabbarcontroller named CustomTabController
).Then in didSelectRowAt
, push tabbarcontroller :
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
let tabController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabController") as! CustomTabController
self.navigationController?.pushViewController(tabController, animated: true)
tab.selectedIndex = indexPath.row
}
else {
// iPad code here
}
}
But make sure, that you have enough number of tabs same as number of rows.