English 中文(简体)
快速倡议:如何让门努·布特顿指挥/脱离接触 有条件
原标题:SwiftUI: How to Enable/Disable Command Menu Button Conditionally

I try to enable/disable Command Menu buttons of the App based on the DetailView appeared or not (view appeared = button enabled). For that I try to use @EnvironmentalObject to receive the current condition via an ViewState class using it to enable/disable the button:

@main
 struct MyApp: App {
  ...
  
  // access viewState via Environmental Object
  @EnvironmentObject var viewState: ViewState

  var body: some Scene {
    WindowGroup {
    ...
    }

    .commands {
        
      // changing "File" menu list
      CommandGroup(after: CommandGroupPlacement.undoRedo) {
        
        Button("Listeneintrag bearbeiten") {
            // do stuff
        }.disabled(!viewState.itemSelected)  // <-- ERROR OCCURING
      }
    }
 }
}

具有变量的观测目标

class ViewState : ObservableObject {

  @Published var userSelected = false
  @Published var itemSelected = false
}

详细情况 观点的出现应确定国家:

struct DetailsView: View {
 ...
 @StateObject var viewState = ViewState()

 var body: some View {
    ZStack(alignment: .bottom) {
  ...
 }
 .onAppear {
    viewState.userSelected = true
 }
 .onDisappear {
    viewState.userSelected = false
 }
}

错误信息是:<条码> 发现的类型观点的目的。 A View.environmentObject (_:) for ViewState may beshed as an ancestor of this view.

My approach might be completely wrong but I used it to share information from App to the views successfully. I did not find any information on how to pass data/information from views to the (main) app struct. Any hint?

问题回答

Your error is telling you that in your App, there is no viewState model. This is because you have to declare your viewState (the single source of truth) in the parent of your view hierarchy, to be passed down to other views. And App is the top most in the view hierarchy.

因此,在你宣布<代码>@时尝试采用这种做法。 国家 申请中反对国=观点国(),并将这一模式传达给您的其他意见。 这使你在申请与其他意见之间有两种方式的<条码>通信,,反之亦称<>。

例:

import SwiftUI

@main
struct MyApp: App {
    @StateObject var viewState = ViewState()  // <--- here
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(viewState) // <--- here
        }
        .commands {
            CommandGroup(after: CommandGroupPlacement.undoRedo) {
                Button("Listeneintrag bearbeiten") {
                    // ...
                }.disabled(!viewState.itemSelected)
            }
        }
    }
}

class ViewState : ObservableObject {
  @Published var userSelected = false
  @Published var itemSelected = false
}

struct ContentView: View {
    
    var body: some View {
        // ...
        DetailsView()
    }
}

struct DetailsView: View {
    @EnvironmentObject var viewState: ViewState  // <--- here
    
    var body: some View {
        VStack {
            Text("DetailsView")
            Button("Enable/Disable Listeneintrag bearbeiten button") { // <-- for testing
                viewState.itemSelected.toggle()
            }
        }
        .onAppear {
            viewState.userSelected = true
        }
        .onDisappear {
            viewState.userSelected = false
        }
    }
}

查阅 官方链接后,它向您提供了一些良好实例,说明如何在你的附录中管理数据:。 请注意,如果你是爱心17,见。 a. 管理你的模型数据。





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...