-
Notifications
You must be signed in to change notification settings - Fork 4
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
[ Question ] How to use path variable in child view #1
Comments
Hello! You can pass a path binding to Then, you can pass path during initialisation of
Now you can use path in |
Also, architecture-wise, I would suggest not to pass path all around the app. It may be fine in small examples like yours, but on a large scale, if views will push and pop uncontrollably, it will be a mess |
Thanks for the quick response. @Alexroar I already tried |
Alright |
Complete working example: import SwiftUI
import PathPresenter
struct ContentView: View {
@State var path = PathPresenter.Path()
var body: some View {
PathPresenter.RoutingView(path: $path){
VStack {
Spacer()
Button("Forgot password") {
path.append(
ForgotPasswordView(path: $path),
type: .animated(
transition: .scale,
animation: .easeIn)
)
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.border(.blue)
}
}
struct ForgotPasswordView: View {
@Binding var path: PathPresenter.Path
var body: some View {
VStack(alignment: .center){
Spacer()
VStack {
Text("Restore password screen")
Button("back") {
path.removeLast()
}
}
.background(.white)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
} |
You need to somehow restrict subviews to be able to do limited actions with path. In your example you do not need to pass the whole path to the subview. You can pass some closure like And inside this closure you will do needed actions with path. Also, check out MVVM-C architecture. I believe it's a nice extension of MVVM that has good control over navigation. |
Also, I'm planning to extend this library with wrapper around path that will restrict views from doing some mess with all other views ;) |
Alright got it. Thanks. |
Can you make it global? so we can use it on every page as an I am using a similar library for SwiftUI navigation but its not working for |
You can make a class wrapper and pass it as class PathObserved: ObservableObject {
@Published var path = PathPresenter.Path()
} But if you need this object in global space, it's a smell of bad code architecture. This library was specifically designed without |
Ah ok. It's hard to deal with Navigation in macOS so I'll have to go for any of this. Can the given class wrapper create a mess for large app having many pages? |
The biggest concern I have is such situation:
Result: view B removed unexpectedly, and view A is not not removed. So, yes. Uncontrollable pushing/popping of views will hurt your app. I will try to address such issue in future releases, creating new structure for managing such situations. |
Ok thanks |
I am following the given example but it is still unclear that how can I use the same
path
variable in child view. The main path variable should be accessible to all its child view and must be binded.I have
LoginView
andForgetPasswordView
. I have declaredpath
as below inLoginView
@State var path = PathPresenter.Path()
and embedded login layout inside PathPresenter like this
and then I am appending
ForgetPasswordView
in this pathbut now the
back button
is inside theForgetPasswordView
so I am wondering how can I use the same path variable to go back or to append any new View?The text was updated successfully, but these errors were encountered: