Search code examples
objective-cxcodensmutablearraynsarray

How can i get data from my mainViewcontroller when subview is present


i have a MainViewController and multiple SubViews in it. i have a NSMutableArray in my MainViewController but i need it inside my SubView so how can i access that mutablearray from my subview when it is present as a child of MainViewController?


Solution

  • Simply create an array property in your subview class and pass the array from your main viewcontroller to your subview class when you initialise your subview.

    Code in your main viewcontroller:

    YourSubviewClass *subView = [[YourSubview alloc] init];
    subView.array = self.yourMainViewControllArray
    

    Code in your subview class header file:

    @interface YourSubviewClass : UIView
    
    @property (nonatomic) NSMutableArray *array;
    

    Hope this helps, good luck.