首页 > 解决方案 > How to update my xib labels from another view controller

问题描述

I am trying to figure out how to update my xib labels when a button is pressed on another view controller

Currently I have a xib swift file that contains all my labels

var habit: Habit? {
    didSet {
        self.updateUI()
    }
}

func updateUI() {
    habitXibName?.text = habit?.title
    totalProgress?.text = habit?.totalCount
    progressBarXib?.progress = habit?.progress ?? 0.0
    userProgress?.text = String(habit?.userCount ?? 0)
}

In another view controller I have the following code

do {
        try realm.write {
            if selectedHabit!.userCount == (Int(selectedHabit!.totalCount )! - 1) {
                selectedHabit!.userCount += 1
                habitcell.habitXibName?.text = String(selectedHabit?.userCount ?? 0)
                view.addSubview(confettiView)
                confettiView.startConfetti()
                DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                    self.confettiView.stopConfetti()
                }
            } else {
                selectedHabit!.userCount += 1
            }
        }
    } catch {
        print(error)
    }

what I want to happen is to update my xib label to the selectedHabit?.userCount

habitcell.habitXibName?.text = String(selectedHabit?.userCount ?? 0)

However, my habitcell.habitXibName returns nil and I'm not sure why. Thanks for everything!

标签: iosswiftrealm

解决方案


推荐阅读