ios – Access properties of a parent class from a child class


I have a parent class MainViewController that hosts two subclasses secondViewController and thirdViewController and I am trying to access a property id of MainViewController from secondViewController like self.parent! because I need to store and save it into a delegate array globally to access it from another UIViewController


class MainViewController: {

 var id: Int

  required init?(coder aDecoder: NSCoder) {
        self.id = 0
        super.init(coder: aDecoder)
    }

//...other subclasses

}
class secondViewController: UIViewController {
    
  
 func savedFunc(){
        let uuid = UUID()

      // saved as a custom struct 
        let prop = viewControllerDictionary(id: uuid, view: self.parent!)
     
        //append into delegate 
        VCs.shared.storedVc.append(prop)
    }
 }

I tried creating an instance of MainViewController inside secondViewController VC like so

 weak var mainViewController: MainViewController?

And access it like this

mainViewController.id 

But it does not work for me because I need to access the path of the current MainViewController parent class not a new instance of it, since I am doing it from a child subclass.
Is there any way to kinda access it like self.parent.id? I know that is a syntax mistake but it is the only way I could think of accessing the current parent class property. How could I make it correctly?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img