根据条件显示 NavigationLink 并在目的地内编辑值

     2023-02-24     266

关键词:

【中文标题】根据条件显示 NavigationLink 并在目的地内编辑值【英文标题】:Show NavigationLink based on condition and having value edited inside the destination 【发布时间】:2020-04-08 05:48:20 【问题描述】:

我的StudentStore 有一个 ForEach 循环,如果isFavorite 字段为真,我只想在我的列表中显示它们。

问题是当我在SecondView 中编辑isFavorite 的值时,由于ContentView 中的if 语句,它会回到ContentView

我希望在我返回 ContentView 时更新列表,而不是立即更新它导致它返回到原始页面。

当我在寻找解决方案时,我想到了这个:

阅读整个线程,以为您在问是否可以有条件地隐藏 NavigationView,然后想:“这听起来很混乱……” 来源:https://www.reddit.com/r/SwiftUI/comments/e2wn09/make_navigationlink_conditional_based_on_bool/

还有人可以解决这个问题吗?为什么有条件地隐藏NavigationView 是个坏主意?

型号

import SwiftUI

struct StudentItem: Identifiable 
    var id = UUID()
    var name: String
    var isFavorite: Bool

    init(name: String, isFavorite: Bool) 
        self.name = name
        self.isFavorite = isFavorite
    


class StudentStore: ObservableObject 
    @Published var StudentStore: [StudentItem]

    init(StudentStore: [StudentItem])
        self.StudentStore = StudentStore
    

主视图

struct ContentView: View 
    @ObservedObject var studentStore = StudentStore(StudentStore: [StudentItem(name: "Stephen", isFavorite: true),
                                                                   StudentItem(name: "Jay", isFavorite: true),
                                                                   StudentItem(name: "Isaac", isFavorite: true),
                                                                   StudentItem(name: "Talha", isFavorite: true),
                                                                   StudentItem(name: "John", isFavorite: true),
                                                                   StudentItem(name: "Matt", isFavorite: true),
                                                                   StudentItem(name: "Leo", isFavorite: true)])

    var body: some View 
        NavigationView 
            List 
                ForEach(studentStore.StudentStore.indices, id: \.self)  index in
                    Group 
                        if self.studentStore.StudentStore[index].isFavorite == true 
                            NavigationLink(destination: SecondView(student: self.$studentStore.StudentStore[index])) 
                                HStack 
                                    Text(self.studentStore.StudentStore[index].name)
                                    Image(systemName: self.studentStore.StudentStore[index].isFavorite ? "star.fill" : "star")
                                
                            
                        
                    
                
            
        
    

子视图

struct SecondView: View 
    @Binding var student: StudentItem

    var body: some View 
        Button(action: 
            self.student.isFavorite.toggle()
        ) 
            Image(systemName: student.isFavorite ? "star.fill" : "star")
        
    

【问题讨论】:

【参考方案1】:

问题是有道理的,它是一个 Binded 值。根据绑定,该人SecondView 的导航在技术上不再存在。

您可以为SecondView 创建一个本地化的State,将您的图像和按钮设置为使用该属性,然后在导航返回时使用onDisappear 更新StudentItem

struct SecondView: View 
    @Binding var student: StudentItem
    @State var isFavorite: Bool = true

    var body: some View 
        Button(action: 
            self.isFavorite.toggle()
        ) 
            Image(systemName: self.isFavorite ? "star.fill" : "star")
        .onDisappear 
            self.student.isFavorite = self.isFavorite
        
    

以上内容将适用于您的其余代码。

至于不使用if 声明,我有点明白。对我来说,我想遍历我知道我想要包含的值。并不是说这是完美的,但您可以在进入之前过滤列表,然后调用 StudentItem 的 Bindable 引用以传递到 SecondView。一定要让 StudentItem Hashable 这个工作:

var body: some View 
        NavigationView 
            List 
                ForEach(studentStore.StudentStore.filter($0.isFavorite == true), id: \.self)  student in
                    Group 
                        NavigationLink(destination: SecondView(student: self.$studentStore.StudentStore[self.studentStore.StudentStore.firstIndex(of: student)!])) 
                                HStack 
                                    Text(student.name)
                                    Image(systemName: student.isFavorite ? "star.fill" : "star")
                                
                            
                        
                
            
        
    

【讨论】:

这会产生更理想的结果,但我希望在list 可见之前进行转换。 我听到了。另一种解决方案是制作自己的后退按钮,类似于解决方案here。这样,您可以在调用解除之前专门更新 Bindable 对象。需要注意的是,我不确定您是否仍想在将值更新为 false 后调用解雇,因为这可能会像根本问题一样“自动解雇”,哈哈。

SwiftUI:NavigationLink 无意中点击并在表单内激活

】SwiftUI:NavigationLink无意中点击并在表单内激活【英文标题】:SwiftUI:NavigationLinkunintentionallytappableandactivatedinsideForm【发布时间】:2020-04-2818:55:40【问题描述】:我似乎有一个问题,NavigationLink被重新格式化为Form。我的NavigationLink... 查看详情

根据条件显示对话框或活动

】根据条件显示对话框或活动【英文标题】:ShowDialogorActivitybasedonCondition【发布时间】:2019-05-0521:16:08【问题描述】:我想在用户第一次使用android应用程序时显示一个对话框或UI。当满足条件时(向应用程序服务器注册完成),... 查看详情

在 SwiftUI 中以编程方式使用 NavigationLink

】在SwiftUI中以编程方式使用NavigationLink【英文标题】:UseNavigationLinkprogrammaticallyinSwiftUI【发布时间】:2020-01-1308:15:57【问题描述】:如果满足某些条件,我正在寻找一种方法,以便在应用启动时从NavigationView“一级”显示我的Watc... 查看详情

presto/athena 有没有办法根据条件获取列名并在 group by 中使用它?

】presto/athena有没有办法根据条件获取列名并在groupby中使用它?【英文标题】:Isthereawayinpresto/athenatogetcolumnnamesbasedonaconditionandusethatingroupby?【发布时间】:2021-01-1417:49:33【问题描述】:在athena/presto中有没有一种方法可以根据条... 查看详情

如何在 SwiftUI 的文本块中嵌入 NavigationLink?

】如何在SwiftUI的文本块中嵌入NavigationLink?【英文标题】:HowdoIembedaNavigationLinkinsideablockoftextinSwiftUI?【发布时间】:2019-07-1802:08:24【问题描述】:我想让SwiftUINavigationLink与文本块内联显示。我该怎么做?这是我想要完成的一个示... 查看详情

从目标视图返回时,SwiftUI NavigationLink 显示为突出显示

】从目标视图返回时,SwiftUINavigationLink显示为突出显示【英文标题】:SwiftUINavigationLinkshowsashighlightedwhengoingbackfromdestinationview【发布时间】:2020-10-0620:54:34【问题描述】:我在Form中有一个NavigationLink(屏幕截图1)。当我单击时,... 查看详情

NavigationLink 显示为灰色且不执行任何操作 [重复]

】NavigationLink显示为灰色且不执行任何操作[重复]【英文标题】:NavigationLinkisgrayedoutanddoesnotperformanyaction[duplicate]【发布时间】:2019-08-2510:48:04【问题描述】:importSwiftUIstructContentView:Viewvarbody:someViewNavigationLink(destination:DetailVie 查看详情

表单/表格中的 SwiftUI 多个 NavigationLink - 条目保持突出显示

】表单/表格中的SwiftUI多个NavigationLink-条目保持突出显示【英文标题】:SwiftUImultipleNavigationLinksinForm/Sheet-entrystayshighlighted【发布时间】:2020-09-1719:33:34【问题描述】:我遇到了Xcode12/iOS14的问题。在带有NavigationView的工作表中使用... 查看详情

在 SwiftUI 中显示 NavigationLink 时过渡动画消失了

】在SwiftUI中显示NavigationLink时过渡动画消失了【英文标题】:TransitionanimationgonewhenpresentingaNavigationLinkinSwiftUI【发布时间】:2020-05-2415:39:50【问题描述】:我有一个带有NavigationLinks的列表。ListForEach(items,id:\\.id)iteminNavigationLink(desti... 查看详情

插入项目之前的条件并在reactjs中显示消息

】插入项目之前的条件并在reactjs中显示消息【英文标题】:Conditionbeforeinsertinganitemandshowmessageinreactjs【发布时间】:2021-04-0413:25:52【问题描述】:我正在用React.JS开发一个应用程序我需要在插入之前添加一个条件。代码:constApp=(... 查看详情

SwiftUI NavigationLink:如何在显示目标视图之前调用函数

】SwiftUINavigationLink:如何在显示目标视图之前调用函数【英文标题】:SwiftUINavigationLink:howtocallafunctionbeforeshowingdestinationview【发布时间】:2020-10-3001:13:58【问题描述】:我想调用一个函数,在点击一个项目之后,在显示目标视图... 查看详情

在 NavigationLink 之后,在 NavigationView 内、TabView 内显示 NavigationBar。 SwiftUI

】在NavigationLink之后,在NavigationView内、TabView内显示NavigationBar。SwiftUI【英文标题】:DisplayingaNavigationBar,insideaNavigationView,insideaTabView,afteraNavigationLink.SwiftUI【发布时间】:2020-03-2203:17:50【问题描述】:我正在制作一个用户需要登... 查看详情

如何根据以下条件将变量从 loginform 传递给 app.js,并在使用 react js 时移动到下一个屏幕

】如何根据以下条件将变量从loginform传递给app.js,并在使用reactjs时移动到下一个屏幕【英文标题】:howtopassthevariablefromloginformtoapp.jsonthebelowcriteriaandmovetothenextscreenwhenitistrueusingreactjs【发布时间】:2021-06-0215:47:37【问题描述】:... 查看详情

在没有以前 NavigationLink 的 SwiftUI NavigationView 上显示系统后退按钮

】在没有以前NavigationLink的SwiftUINavigationView上显示系统后退按钮【英文标题】:DisplaysystembackbuttononSwiftUINavigationViewwithnopreviousNavigationLink【发布时间】:2020-09-0309:27:44【问题描述】:我有一个自定义的SwiftUI标题视图,它由几个Text... 查看详情

没有 NavigationLink 的 SwiftUI 列表披露指示器

】没有NavigationLink的SwiftUI列表披露指示器【英文标题】:SwiftUIListdisclosureindicatorwithoutNavigationLink【发布时间】:2020-09-2601:09:13【问题描述】:我正在寻找一种解决方案来显示披露指示器V形,而无需将我的视图包装到NavigationLink中... 查看详情

NavigationLink SwiftUI 中的三元运算符

】NavigationLinkSwiftUI中的三元运算符【英文标题】:TernaryoperatorinsideNavigationLinkSwiftUI【发布时间】:2020-09-2713:56:37【问题描述】:尝试在NavigationLink中的按钮上使用三元运算符时遇到问题。我有一系列活动,显示在轮播视图中。Bell... 查看详情

NavigationLink 中的 SwiftUI 按钮

】NavigationLink中的SwiftUI按钮【英文标题】:SwiftUIButtoninsideaNavigationLink【发布时间】:2020-04-1113:40:30【问题描述】:我有一个列表项视图,它显示有关嵌入在navigationLink中的任务的一些基本信息。我想使用navigationLink中的按钮来切... 查看详情

如何根据“枚举”列获得分类的 laravel 雄辩结果并在一页中显示?

】如何根据“枚举”列获得分类的laravel雄辩结果并在一页中显示?【英文标题】:Howtogetcategorizedlaraveleloquentresultsbasedon"enum"columnandshowtheminonepage?【发布时间】:2018-08-0213:13:54【问题描述】:为了简单起见,我将解释一个... 查看详情