选择器内的 SwiftUI 无限循环

Posted

技术标签:

【中文标题】选择器内的 SwiftUI 无限循环【英文标题】:SwiftUI Infinite loops inside picker 【发布时间】:2019-11-24 19:25:56 【问题描述】:

尝试执行函数但返回“某些视图”时遇到问题。我也在尝试使用 continue 语句执行 while 循环,但这在 viewBuilder 的主体中是不允许的...当条件发生变化时使用选择器的最简单方法是什么?

var body: some View 
    VStack
        Group
            Form 
                Section(header: Text("\(screeningTable())")) 
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) 
                        List(maleDataModel.ageArray, id: \.self)  i in
                            Text("\(i, specifier: "%g")-years-old")
                        
                    

                if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 
                    Section 
                        Text("Fill all required fields").foregroundColor(.red)
                    
                

                if screeningTable() == "No Go" 
                    Section(header: Text("Tape Mesurements")) 
                        MaleTapeView()
                    
                
                if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"
                    Section(header: Text("You are not required to tape").foregroundColor(.blue)) 
                        MaleSaveButton()
                    
                
            
        

【问题讨论】:

在这里查看这个答案***.com/questions/58676483/… 【参考方案1】:

我不确定您的代码是否有效,但如果您想要 if 语句,可以将它们包含在 ZStacks 中

var body: some View 
    VStack
        Group
            Form 
                Section(header: Text("\(screeningTable())")) 
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) 
                        List(maleDataModel.ageArray, id: \.self)  i in
                            Text("\(i, specifier: "%g")-years-old")
                        
                    
                ZStack 
                    if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 
                        Section 
                            Text("Fill all required fields").foregroundColor(.red)
                        
                    
                
                ZStack 
                    if screeningTable() == "No Go" 
                        Section(header: Text("Tape Mesurements")) 
                            MaleTapeView()
                        
                    
                
                ZStack 
                    if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"
                        Section(header: Text("You are not required to tape").foregroundColor(.blue)) 
                        MaleSaveButton()
                    
                
            
        
    

【讨论】:

【参考方案2】:

我从设计 + 代码课程中得到了这个。我可以看到@State 属性如何在没有无穷无尽的条件语句的情况下来回切换......

            BottomCardView(showRing: $showCard)
            .offset(y: showCard ? screen.height/2 - 50 : screen.height)
            .offset(y: bottomState.height)
            .blur(radius: show ? 20 : 0)
            .animation(.spring(response: 0.5, dampingFraction: 0.7, blendDuration: 0))
            .gesture(
                DragGesture()
                    .onChanged  value in
                        self.bottomState = value.translation
                        if self.showFull 
                            self.bottomState.height += -300
                        
                        if self.bottomState.height < -300 
                            self.bottomState.height = -300
                        
                
                .onEnded  value in
                    if self.bottomState.height > 50 
                        self.showCard = false
                    
                    if (self.bottomState.height < -100 && !self.showFull) || (self.bottomState.height < -250 && self.showFull) 
                        self.bottomState.height = -300
                        self.showFull = true
                     else 
                        self.bottomState = .zero
                        self.showFull = false
                    
                
        )

【讨论】:

以上是关于选择器内的 SwiftUI 无限循环的主要内容,如果未能解决你的问题,请参考以下文章

在 SwiftUI 中使用 onAppear 时出现无限循环

无限while循环内的Sleep()函数

iPhone:在 UIScrollView 内无限循环内容

无限for循环内的goroutine。这是一个好习惯吗?

使用 useEffect 反应 axios 获取请求而不会导致无限循环

C/C++ 中的无限循环 [关闭]