Ios 将.environmentObject与以工作表形式显示的视图一起使用会导致onReceive不触发/与@environmentObject冲突
在我的Ios 将.environmentObject与以工作表形式显示的视图一起使用会导致onReceive不触发/与@environmentObject冲突,ios,swift,swiftui,Ios,Swift,Swiftui,在我的ContentView中,我有一个按钮,用于显示我的设置视图的简单工作表。当视图以模式显示时,与my@Environment Object var iconSettings:IconNames中的my@Environment var iconSettings:IconNames似乎存在一些冲突,其中myonReceive函数仅在视图首次加载时触发,而在使用选择器时从不触发 环顾四周寻找与此相关的答案,我只能找到一些与CoreData相关的东西,这些东西并没有真正的帮助,但我相信其他人也经历
ContentView
中,我有一个按钮,用于显示我的设置视图的简单工作表。当视图以模式显示时,与my@Environment Object var iconSettings:IconNames
中的my@Environment var iconSettings:IconNames
似乎存在一些冲突,其中myonReceive
函数仅在视图首次加载时触发,而在使用选择器时从不触发
环顾四周寻找与此相关的答案,我只能找到一些与CoreData相关的东西,这些东西并没有真正的帮助,但我相信其他人也经历过这一点,所以如果有一些规范的、更一般的东西供其他人参考,那将是非常棒的
谢谢
Button(action: { self.modalDisplayed = true }) {
Assets.gear
}.sheet(isPresented: $modalDisplayed) {
SettingsView(state: self.state, loadCards: self.load)
.environmentObject(IconNames())
}
在我的设置视图中,我有以下内容:
struct SettingsView: View {
@ObservedObject var state: AppState
@EnvironmentObject var iconSettings: IconNames
let loadCards: () -> Void
var body: some View {
VStack {
Picker("", selection: $iconSettings.currentIndex) {
ForEach(Publication.allCases, id: \.pubId) {
Text($0.pubName).tag($0.pubId)
}
}
.pickerStyle(SegmentedPickerStyle())
.padding(20)
Spacer()
}
.onReceive([self.iconSettings.currentIndex].publisher.first()) { value in
print(value) // only hits on first load, never on tap
print("")
let index = self.iconSettings.iconNames.firstIndex(of: UIApplication.shared.alternateIconName) ?? 0
if index != value { UIApplication.shared.setAlternateIconName(self.iconSettings.iconNames[value]) { error in
if let error = error {
print(error.localizedDescription)
} else {
print("Success!")
}
}
}
}
}
}
最后,我的IconNames课程:
class IconNames: ObservableObject {
var iconNames: [String?] = [nil]
@Published var currentIndex = 0
init() {
getAlternateIconNames()
if let currentIcon = UIApplication.shared.alternateIconName {
self.currentIndex = iconNames.firstIndex(of: currentIcon) ?? 0
}
}
func getAlternateIconNames() {
if let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
let alternateIcons = icons["CFBundleAlternateIcons"] as? [String: Any]
{
for (_, value) in alternateIcons{
guard let iconList = value as? Dictionary<String,Any> else{return}
guard let iconFiles = iconList["CFBundleIconFiles"] as? [String]
else{return}
guard let icon = iconFiles.first else{return}
iconNames.append(icon)
}
}
}
}
class图标名:observeObject{
变量图标名称:[字符串?]=[nil]
@已发布变量currentIndex=0
init(){
getAlternateConNames()
如果让currentIcon=UIApplication.shared.AlternateConName{
self.currentIndex=iconNames.firstIndex(of:currentIcon)??0
}
}
func getAlternateConNames(){
如果让icons=Bundle.main.object(forInfoDictionaryKey:“Cbundleicons”)作为?[String:Any],
让AlternateCons=图标[“CbundLeaAlternateCons”]作为?[字符串:任意]
{
用于替换项中的(_,值){
guard let iconList=值为?Dictionary else{return}
guard let iconFiles=iconList[“CbundleIConfiles”]作为?[String]
else{return}
guard let icon=iconfile.first-else{return}
iconNames.append(图标)
}
}
}
}
好吧,由于提供的代码快照不可通过复制粘贴进行测试,因此只能通过读取代码来测试,请尝试,而不是
.onReceive([self.iconSettings.currentIndex].publisher.first()) { value in
用这个
.onReceive(self.iconSettings.$currentIndex) { value in