在SwiftUI中是否有创建视图链的方法?

huangapple go评论72阅读模式
英文:

Is there a way to create a chain of views in SwiftUI?

问题

Sure, here are the translated parts of your content:

我想在SwiftUI中创建一系列的视图,我之前使用了 NavigationView 但已经被弃用了,iOS 16.0 中是否有一些替代方案?

view1 -> view2 -> view3 -> view4 -> root(view3)

我已阅读关于 NavegationStack 的信息,但不太清楚,它好像是用来实现列表和详细项的。

enum Screens {
    case view1
    case view2
}

extension Screens: Hashable {}

final class SettingsNavigationManager: ObservableObject {
    
    @Published var screen: Screens? {
        didSet {
            print("🔑 \(screen)")
        }
    }
    
    func push(to screen: Screens) {
        self.screen = screen
    }
    
    func popToRoot() {
        self.screen = nil
    }
}

struct viewOne: View {
    @EnvironmentObject var settingsNavManager: SettingsNavigationManager
    var body: some View{
        Button("前往ViewTwo") {
            settingsNavManager.push(to: .view2)
        }
        .background(
            NavigationLink(
                destination: viewTwo(),
                tag: .view2,
                selection: $settingsNavManager.screen) { EmptyView() }
        )
    }
}

Please note that I've translated the code and some key phrases while omitting the parts you requested not to translate.

英文:

I want to create a chain of views in SwiftUI, I was using NavigationView but was deprecated, are there some alternatives in iOS 16.0?

view1 -> view2 -> view3 -> view4 -> root(view3)

在SwiftUI中是否有创建视图链的方法?

enum Screens {
    case view1
    case view2
}

extension Screens: Hashable {}

final class SettingsNavigationManager: ObservableObject {
    
    @Published var screen: Screens? {
        didSet {
            print("📱 \(screen)")
        }
    }
    
    func push(to screen: Screens) {
        self.screen = screen
    }
    
    func popToRoot() {
        self.screen = nil
    }
}

struct viewOne: View {
    @EnvironmentObject var settingsNavManager: SettingsNavigationManager
    var body: some View{
        Button("Go to ViewTwo") {
            settingsNavManager.push(to: .view2)
        }
        .background(
            NavigationLink(
                destination: viewTwo(),
                tag: .view2,
                selection: $settingsNavManager.screen) { EmptyView() }
        )
    }
}

I have read about NavegationStack but idk, it's like to implements list and details items.

答案1

得分: 1

https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types

你可以只使用 navigationPath 对象在你的模型中,它会为你管理推入和弹出。

我想在SwiftUI中创建一系列视图,我曾经使用 NavigationView,但它已被弃用,iOS 16.0 中有替代方案吗?

view1 -> view2 -> view3 -> view4 -> 根视图(view3)

enum Screens { case view1 case view2 }

extension Screens: Hashable {}

final class SettingsNavigationManager: ObservableObject {
@Published var path: NavigationPath }

然后,你可以使用以下方式初始化导航栈

NavigationStack(path: $settingsNavManager.path){ }

然后,你可以使用以下方式进行推入和弹出操作

settingsNavigationManager.path.append()

removeLast(settingsNavManager.path.count)

作为推入和弹出到根视图的操作。

你还可以使用 navigationdestination 根据你的 screens 枚举来调整应该调用哪个视图。

.navigationDestination(for: Screens.self){ screen in
switch screen{
} }

英文:

https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types

you can just use navigationPath object in your model which will manage push and pop for you.

I want to create a chain of views in SwiftUI, I was using NavigationView but was deprecated, are there some alternatives in iOS 16.0 ?

view1 -> view2 -> view3 -> view4 -> root(view3)

enter image description here

> enum Screens { case view1 case view2 }
>
> extension Screens: Hashable {}
>
> final class SettingsNavigationManager: ObservableObject {
> @Published var path: NavigationPath }

then you initialize navigationstack with

> NavigationStack(path: $settingsNavManager.path){ }

Then you can use

> settingsNavigationManager.path.append()

and

> removeLast(settingsNavManager.path.count)

as push and popToRoot

you can also use navigationdestination to adjust which view should be called according to your screens enum

> .navigationDestination(for: Screens.self){ screen in
> switch screen{
> } }

huangapple
  • 本文由 发表于 2023年5月10日 12:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定