Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to manage routes for tab bar based application? #46

Open
iosYash opened this issue Jun 15, 2023 · 1 comment
Open

How to manage routes for tab bar based application? #46

iosYash opened this issue Jun 15, 2023 · 1 comment

Comments

@iosYash
Copy link

iosYash commented Jun 15, 2023

I have one question, Do I have to create individual route for each tab?

If yes, then suppose there is one view which is part of 3rd tab but I want to push that view in 2nd tab. How can I push 3rd routes view to using 2nd route.

@cp-amisha-i
Copy link
Collaborator

cp-amisha-i commented Jan 3, 2024

Hello @iosYash, Yes we have to create individual routes for each tab.
The main route for the whole app, and other routes for individual tabs.

It's also very easy to show the view of one tab in another tab as we have a different router for each tab, we can create a new case in the enum switch of the one tab's router to open the view of the other tab's view, and just use the same view for both tab in the switch case.

Let me explain it with an example,
I have used 2 tabs, and we want to open 1 same screen from both tabs,

public enum AppRoute: Equatable {
	public static func == (lhs: AppRoute, rhs: AppRoute) -> Bool {
		return lhs.key == rhs.key
	}
	
	// MARK: - Home tab
	case Home
	
	// MARK: - Settings tab
	case Setting
	case ReportProblem
	
	public var key: String {
		switch self {
		case .Home:
			return "homeView"
		case .Setting:
			return "settingView"
		case .ReportProblem:
			return "reportProblemView"
		}
	}
}

struct HomeTabRouteView: View {
	@StateObject var tabPilot = UIPilot(initial: AppRoute.Home)
	
	var body: some View {
		VStack(spacing: 0) {
			UIPilotHost(tabPilot) { route in
				switch route {
				case .Home:
					HomeView()
				case .ReportProblem:
					ReportProblemView()
				default:
					EmptyView()
				}
			}
		}
	}
}

struct SettingTabRouteView: View {
	@StateObject var tabPilot = UIPilot(initial: AppRoute.Home)
	
	var body: some View {
		VStack(spacing: 0) {
			UIPilotHost(tabPilot) { route in
				switch route {
				case .Setting:
					SettingView()
				case .ReportProblem:
					ReportProblemView()
				default:
					EmptyView()
				}
			}
		}
	}
}

Using this we can open the report problem screen (which is part of the 2nd tab) in the 1st tab.
Note:- We can not switch the tab programmatically to use the same view in a different tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants