首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定值只更改一次。

绑定值只更改一次。
EN

Stack Overflow用户
提问于 2022-11-08 09:30:23
回答 1查看 39关注 0票数 0

我有下面的View结构,当我第一次单击并禁用该按钮时,会遇到按钮工作的问题,但是一旦我单击一个按钮,它就不适用于其他按钮。这个项目总是正确的,我是通过打印出来来检查的。

我的ReelsView

代码语言:javascript
复制
struct ReelsView: View {
    @State var currentReel = ""
    @State var items: [Item] = [
        Item(chance: "1:1m", tradeIn: 2000, name: "Apple Watch", price: "$200", rarityType: "common", description: "The Apple Watch", reel: Reel(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "apple-watch", ofType: "mp4") ?? "")), bid: false)),
        Item(chance: "1:20m", tradeIn: 27500, name: "Ibiza vacation", price: "$2750,00", rarityType: "superRare", description: "Such a wonderful place for a vacation", reel: Reel(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "ibiza", ofType: "mp4") ?? "")), bid: false)),
    ]
    var body: some View {
        TabView(selection: $currentReel) {
            ForEach($items) { $reel in
                ReelsPlayer(reel: $reel.reel, currentReel: $currentReel, item: $reel)
                    .tag(reel.reel.id)
            }
        }
    }
}

我的ReelsPlayer

代码语言:javascript
复制
struct ReelsPlayer: View {
    @Binding var reel: Reel
    @Binding var currentReel: String
    @Binding var item: Item
    var body: some View {
        ZStack {
            if let player = reel.player {
                CustomVideoPlayer(player: player)
                    .allowsHitTesting(false)
            }
        }
        .overlay {
            BottomOverlay(item: $item)
                .allowsHitTesting(true)
        }
    }
}

我的BottomOverlay

代码语言:javascript
复制
struct BottomOverlay: View {
    @Binding var item: Item
    var body: some View {
        Button() {
            item.reel.bid.toggle()
            print("item: ", item)
            print("item: ", $item)
        } label: {
            Text(item.reel.bid ? "Already Bid" : "Bid")
        }
    }
}
代码语言:javascript
复制
struct Reel: Identifiable {
    var id = UUID().uuidString
    var player: AVPlayer
    var bid: Bool
}
代码语言:javascript
复制
struct Item: Identifiable, Hashable {
    static func == (lhs: Item, rhs: Item) -> Bool {
        lhs.id == rhs.id
    }
    
    func hash(into hasher: inout Hasher) {
            hasher.combine(chance)
            hasher.combine(name)
            hasher.combine(price)
            hasher.combine(tradeIn)
            hasher.combine(rarityType)
            hasher.combine(description)
        }
    
    var id: String = UUID().uuidString
    var chance: String
    var tradeIn: Int
    var name: String
    var price: String
    var rarityType: String
    var description: String
    var reel: Reel
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-08 09:55:23

下面是我在测试中使用的代码,以显示这两个按钮分别工作。单击一个,然后打印项id和状态。单击另一个,与该项目相同。如果您取消注释.disabled(item.clicked),那么它只能工作一次,因为按钮(对于该项)现在是禁用的。

代码语言:javascript
复制
struct Item: Identifiable {
    let id = UUID()
    var name: String
    var clicked: Bool
}

struct ContentView: View {
    var body: some View {
        MainView()
    }
}

struct MainView: View {
    @State var items: [Item] = [Item(name: "item1", clicked: false),Item(name: "item2", clicked: false)]
    
    var body: some View {
        ForEach($items) { $item in
            ItemView(item: $item)
        }
    }
}

struct ItemView: View {
    @Binding var item: Item
    var body: some View {
        VStack {
            Color.green
        }.overlay {
            OverlayView(item: $item)
        }
    }
}

struct OverlayView: View {
    @Binding var item: Item
    var body: some View {
        VStack (spacing: 33){
            Button() {
                item.clicked.toggle()
                print("--> item.id: \(item)  item.clicked: \(item.clicked)")
            } label: {
                Text(item.name)
            }//.disabled(item.clicked)
            //            Button() {
            //                item.clicked.toggle()
            //            } label: {
            //                Text("enable again")
            //            }
        }
    }
}

编辑-1:鉴于您的新代码。试试这个示例代码,对我来说很好。

代码语言:javascript
复制
import Foundation
import SwiftUI
import UIKit
import AVFoundation
import AVKit


struct ContentView: View {
    var body: some View {
        ReelsView()
    }
}

struct ReelsView: View {
    @State var currentReel = ""
    @State var items: [Item] = [
        Item(chance: "1:1m", tradeIn: 2000, name: "Apple Watch", price: "$200", rarityType: "common", description: "The Apple Watch", reel: Reel(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "apple-watch", ofType: "mp4") ?? "")), bid: false)),
        Item(chance: "1:20m", tradeIn: 27500, name: "Ibiza vacation", price: "$2750,00", rarityType: "superRare", description: "Such a wonderful place for a vacation", reel: Reel(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "ibiza", ofType: "mp4") ?? "")), bid: false)),
    ]
    
    var body: some View {
        TabView(selection: $currentReel) {
            ForEach($items) { $item in
                ReelsPlayer(currentReel: $currentReel, item: $item) // <-- here
                    .tag(item.reel.id)
            }
        }.tabViewStyle(.page)  // <-- here
    }
}

struct ReelsPlayer: View {
    @Binding var currentReel: String
    @Binding var item: Item  // <-- here
    var body: some View {
        ZStack {
            if let player = item.reel.player {  // <-- here
                //  CustomVideoPlayer(player: player)
                // for testing
                VStack {
                    if item.name == "Apple Watch" { Color.yellow } else { Color.green }
                }
                .allowsHitTesting(false)
            }
        }
        .overlay {
            BottomOverlay(item: $item)
                .allowsHitTesting(true)
        }
    }
}

struct BottomOverlay: View {
    @Binding var item: Item
    var body: some View {
        Button() {
            item.reel.bid.toggle()
            print("----> BottomOverlay item.reel.bid: ", item.reel.bid)  // <-- here
        } label: {
            Text(item.reel.bid ? "Already Bid" : "Bid")
        }
    }
}

struct Reel: Identifiable, Hashable {  // <-- here
    var id = UUID().uuidString
    var player: AVPlayer
    var bid: Bool
}

struct Item: Identifiable, Hashable {  // <-- here
    var id: String = UUID().uuidString
    var chance: String
    var tradeIn: Int
    var name: String
    var price: String
    var rarityType: String
    var description: String
    var reel: Reel
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74358415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档