Access NSMutableArray from another class - Objective C

前端 未结 5 1809
清歌不尽
清歌不尽 2020-12-22 06:34

I have a main ViewController that contains a desginated class. Within that ViewController there is a Container that is linked to an <

5条回答
  •  情深已故
    2020-12-22 06:59

    To access the NSMutableArray from one class to another class use following code.

    In the first view controller in which u have declared the object of NSMutableArray, declare the property and synthesize for the same as below,

    //In FirstViewcontroller.h class,

    @property (nonatomic, strong) NSMutableArray *arrData;
    

    //In FirstViewcontroller.m class

    @synthesize arrData;
    

    Also FirstViewcontroller object should be global so you can create the object of FirstViewcontroller in app delegate file.

    //appdelegate.h

    @property (nonatomic, strong) FirstViewcontroller *objFirst;
    

    //appdelegate.m

    @synthesize objFirst;
    
    FirstViewcontroller *objFirst=[[FirstViewcontroller alloc]init];
    

    Now in SecondViewcontroller in which you have to access array, create the share object of Appdelegate file

    //SecondViewcontroller.m

     AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    

    Then use will get the required array as below,

    app.objFirst.arrData
    

    This is your required array I hope it will help you.

提交回复
热议问题