Transfer From Parent To Child

In my parent I have a function that uses 4 sliders from each child class, and it calculates return on assets. How do I move this method to the child classes so it can update when a slider moves?

Code Block
func calROA() {
    let ans = Double(revNum - costNum)/Double(fixANum + curANum)
    let perAns = ans * 100
    print("this is the value for revenue " + "\(revNum)")
    print("this is the value for costs " + "\(costNum)")
    print("this is the value for fixed assets " + "\(fixANum)")
    print("this is the value for current assets " + "\(curANum)")
    print("this is the roa " + "\(perAns)")
    roaLabel.text = String(format: "%.1f%%", perAns)
}

Answered by ZoneX in 631887022
Yeah I was able to get it to work finally with notifications and observers. I made one of each for my calROA() method.

How do I move this method to the child classes so it can update when a slider moves?

It is very hard to say how without more context.
  • How your parent class and child classes are defined?

  • How a slider is used in each child class?

  • How parent class and child class are related?

Can you show more context?
My child class is a container of my parent class and it contains a slider that moves a view that updates a label with a number.

My child class is a container of my parent class and it contains a slider that moves a view that updates a label with a number.

Still hard to say something. Can you show more code?
Okay this is the code for one of my sliders in a child view. I want the calROA() method from the parent to be avaliable in the cAssMove function.

Code Block
@IBAction func cAssMove(_ sender: Any) {
    cAssetsHeight.constant = CGFloat(cAssetsSlider.value)
    scaleCA = Int(6.75675675676 * cAssetsSlider.value)
    cAssetsNum.text = "\(scaleCA)"
    if scaleCA < 316 {
      cAssetsLabel.text = ""
    } else {
      cAssetsLabel.text = "Current \n Assets"
    }    
  }

We do not het a global view of your code architecture.

But instead of moving code, why don't you post notification from paren to chic when slider changes ?

Okay this is the code for one of my sliders in a child view

Isn't it a view controller? Without showing your full code, you need to use strictly right terms. Distinguish view controller and view.
And you are still not showing the relationship between parent and child. The children are embedded in the parent? Or some sort of transition occurs from parent to child?

To solve your issue, all such info are needed.
It's in its own vc, I have 4 container viewcontrollers that are embedded in the parent. I'm gonna try notification and observer. I won't confuse view and viewcontroller again.

I'm gonna try notification and observer.

It seems to be one possible solution under the current info. Please tell us if it work for your app.
Accepted Answer
Yeah I was able to get it to work finally with notifications and observers. I made one of each for my calROA() method.
Thanks for feedback. Happy to see the solution I proposed worked here (no surprise).
Transfer From Parent To Child
 
 
Q