@ObservedObject不会将更改发布到View

时间:2020-07-11 02:47:46

标签: ios swift swiftui

我有一个View和一个Text,它打算根据一个CountDown()secondsLeft进行更新:

struct TimerCapsule: View {
    @ObservedObject var countdown = CountDown()
    var body: some View {
        Text("\(self.countdown.secondsLeft)")
            .font(.system(size: 30))
            .foregroundColor(.white)
            .frame(width: 100, height: 40)
            .padding(EdgeInsets(top: 5, leading: 15, bottom: 5, trailing: 15))
            .background(
                Capsule()
                    .fill(Color("Exit"))
                    .opacity(0.7)
            )
            .onAppear{
                print("Start countdown")
                self.countdown.start(from: 30)
            }
            .onDisappear {
                self.countdown.stop()
            }
    }
}

这是下面的CountDown类。 print("Seconds left: \(self.secondsLeft)")成功打印了30、29、28等-但是,以上View中并未更新。上面的视图中仅使用了1个实例,因此我不确定为什么它没有反映在上面的Text("\(self.countdown.secondsLeft)")中。

计时器:

class CountDown: ObservableObject {
    @Published var secondsLeft = 30
    @ObservedObject var block = Block()
    var timer: Timer!
    func start(from seconds: Int){
        self.secondsLeft = seconds
        print("start timer")
        self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true){ _ in
            if (self.secondsLeft == 1){
                switch self.block.type {
                case .match:
                    self.block.pass()
                case .pass:
                    self.block.pass()
                default:
                    self.block.delete()
                }
            }
            self.secondsLeft -= 1
            print("Seconds left: \(self.secondsLeft)")
        }
    }
    func stop(){
        self.timer?.invalidate()
    }
}

有什么主意吗?

编辑:

ContentView.swift

struct ContentView: View {
    @ObservedObject var auth = UserAuth()
    var body: some View {
        Group {
            if auth.uid != nil { HomeView(auth: auth) } else { LoginView(auth: auth) }
        }
    }
}

HomeView.swift

struct HomeView: View {
    let auth: UserAuth
    @ObservedObject var block = Block()
    init(auth: UserAuth) {
        self.auth = auth
        UITabBar.appearance().barTintColor = UIColor.orange
        UITabBar.appearance().unselectedItemTintColor = UIColor.white.withAlphaComponent(0.5)
        UITabBar.appearance().autoresizesSubviews = true
    }
    @State var selectedTab = 1
    @ObservedObject var locationManager = LocationManager()
        
    var body: some View {
        ZStack {
            TabView(selection: $selectedTab) {
                Settings(auth: auth)
                .tabItem {
                        Image(systemName: "gear")
                    Text("Settings")
    
                }.tag(0)
                ZStack {
                    MapView(locationManager: locationManager)
                    Color.black.opacity(self.block.status.opacity).animation(nil)
                    VStack {
                    if self.block.type == Type.match || self.block.type == Type.pass {
                        VStack{
                            TimerCapsule()
                        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
                        .padding(.top, 20)
                    }
                        Spacer()
                        if self.block.type == Type.create || self.block.type == Type.delete || self.block.type == Type.pass{
                        StatusButton()
                        }
                    }.padding(40)
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                }.edgesIgnoringSafeArea(.top)
                .tabItem {
                    Image(systemName: "location.circle.fill")
                    Text("Home")
                }.tag(1)
                ProfileView()
                .tabItem {
                    Image(systemName: "person")
                    Text("Profile")
                }.tag(2)
            }.accentColor(Color.white)
            .font(.headline)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

无法重现任何问题。您没有提供足够的信息来编译整个代码,但是如果我们删除对您遗漏的所有内容的引用,则会得到以下信息:

class CountDown: ObservableObject {
    @Published var secondsLeft = 30
    var timer: Timer!
    func start(from seconds: Int){
        self.secondsLeft = seconds
        print("start timer")
        self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true){ _ in
            // don't know what Block is
            self.secondsLeft -= 1
            print("Seconds left: \(self.secondsLeft)")
        }
    }
    func stop(){
        self.timer?.invalidate()
    }
}

struct ContentView: View {
    @ObservedObject var countdown = CountDown()
    var body: some View {
        Text("\(self.countdown.secondsLeft)")
            .font(.system(size: 30))
            .foregroundColor(.white)
            .frame(width: 100, height: 40)
            .padding(EdgeInsets(top: 5, leading: 15, bottom: 5, trailing: 15))
            .background(
                Capsule()
                    .fill(Color(.black)) // don't know what Exit is
                    .opacity(0.7)
            )
            .onAppear{
                print("Start countdown")
                self.countdown.start(from: 30)
            }
            .onDisappear {
                self.countdown.stop()
            }
    }
}

将其复制并粘贴到新的SwiftUI项目中,您会发现文本的计数倒计时了。

因此,如果有某种原因阻止了这种情况的发生,那是您没有展示的东西。