【问题标题】:Change view without Navigation link SwiftUI在没有导航链接的情况下更改视图 SwiftUI
【发布时间】:2020-09-27 12:14:34
【问题描述】:

我大家!我花了几个小时寻找一些我认为非常简单的东西,但我无法找到最好的方法...... 我有我的身体视图:

var body: some View {
        
        VStack {
            // The CircularMenu
            CircularMenu(menuItems: homeMenuItems, menuRadius: 55, menuButtonSize: 55, menuButtonColor: .black, buttonClickCompletion: buttonClickHandler(_:))
                .buttonStyle(PlainButtonStyle())

        }
    }

其中包含一个圆形菜单。每次点击菜单项都会调用:

func buttonClickHandler(_ index: Int) {
        /// Your actions here
        
        switch index {
        //Thermometer
        case 0:
            print("0")
        //Light
        case 1:
            print("1")
        //Video
        case 2:
            print("2")
        //Alarm
        case 3:
            print("3")
        //Car
        case 4:
            self.destinationViewType = .car
            self.nextView(destination: .car)
            
        default:
            print("not found")
        }
    }

我想执行一个简单的视图转换到另一个名为 Car 的视图。 nextView 函数如下所示:

func nextView(destination: DestinationViewType) {
        
        switch destination {
            case .car: Car()
        }
    }

我认为这很简单,但我在案例行中得到:Result of 'Car' initializer is unused。 那么有人知道如何实现吗?提前非常感谢!

【问题讨论】:

标签: swiftui segue swiftui-navigationlink


【解决方案1】:

这是一种方法:

创建一个名为IdentifiableViewstruct,其中包含一个AnyView 和一个id

struct IdentifiableView: Identifiable {
    let view: AnyView
    let id = UUID()
}

创建一个@State 变量来保存nextView。使用 .fullScreenCover(item:) 显示nextView

@State private var nextView: IdentifiableView? = nil

var body: some View {
        
        VStack {
            // The CircularMenu
            CircularMenu(menuItems: homeMenuItems, menuRadius: 55, menuButtonSize: 55, menuButtonColor: .black, buttonClickCompletion: buttonClickHandler(_:))
                .buttonStyle(PlainButtonStyle())

        }.fullScreenCover(item: self.$nextView, onDismiss: { nextView = nil}) { view in
            view.view
        }
}

然后,将self.nextView 分配给IdentifiableView

case .car: self.nextView = IdentifiableView(view: AnyView(Car()))

返回MenuView 时,请使用self.presentationMode.wrappedValue.dismiss() 关闭视图。下面是一个最小的Car 视图示例:

struct Car: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        Text("Car View").onTapGesture {
            self.presentationMode.wrappedValue.dismiss()
        }
    }
}

【讨论】:

  • 非常感谢@vacawama。你的解决方案真的很棒!!我选择你的解决方案是最好的。非常感谢大家,我很感激
【解决方案2】:

如果你想用新视图完全替换正文内容,你需要一些条件。假设您有一个 Container 和一个 body,如果创建了一个 Car 视图,我们将显示它:

struct Container: View {

    @State var car: Car?      // Storage for optional Car view

    var body: some View {
        if let car = car {    // if the car view exists
            car               // returning the car view
        } else {              // otherwise returning the circular menu
            VStack {
                CircularMenu(menuItems: homeMenuItems, menuRadius: 55, menuButtonSize: 55, menuButtonColor: .black, buttonClickCompletion: buttonClickHandler(_:))
                .buttonStyle(PlainButtonStyle())
            }
        }
    }
...

然后我们只需要在点击时分配新创建的汽车视图实例:

...
    func buttonClickHandler(_ index: Int) {
        switch index {
        ....
        //Car
        case 4:
        car = Car()         // assigning the newly created instance
        ...
        }
    }
}

我看到您提到了destinationViewTyp 和其他一些情况。所以你的代码会比这稍微复杂一些,但想法是一样的。我们存储视图或一些有助于我们在必要时创建视图的信息,然后根据条件返回选择器或视图。

【讨论】:

  • 非常感谢@Denis。您的解决方案真的很棒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
相关资源
最近更新 更多