Access NSMutableArray from another class - Objective C

前端 未结 5 1798
清歌不尽
清歌不尽 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:44

    You seem to be unclear on the difference between classes and instances. OK, so, say we have two NSArrays:

    NSArray *a = [[NSArray alloc] initWithObjects:@"hello", @"I", @"am", @"an", @"array", nil];
    NSArray *b = [[NSArray alloc] initWithObjects:@"so", @"am", @"I", nil];
    

    If I do a.count, I'll get 5 as the answer because the array contains five objects. Meanwhile, if I do b.count, I'll get 3, because that array contains three objects. It isn't that creating b "gets rid of the already set count". They are separate objects completely unrelated to each other.

    Your view controller class is the same way. When you create a different instance, it doesn't overwrite the old one -- it's just not the same object. In order to use the original view controller object, you need to get a reference to it.

    So how do you get a reference to it? Well, the general answer is you design your app so that the two objects know about each other. There are lots of specific ways to accomplish this. A lot of people will say "Just stick a reference in the app delegate." That is one thing you can do, but it's not always the best choice. It can get out of control if you just stick everything in your app delegate. Sometimes it's the right answer, often other things are the right answer. Another approach is to have an object that knows about both of those objects introduce them to each other. But sometimes there is no such object. So it's situational.

    0 讨论(0)
  • 2020-12-22 06:44

    The basic idea here is that in your original class, the array is referred to by a pointer. Your original class would allocate it and presumably load it. Other parts of your program can be handed the contents of the property, which is a pointer, assign that to their own pointer holder, and use it as if you had declared it there. Please use the above code;

    MyClass *aClass = [[MyClass alloc] initWithMyInitStuff];
    NSMutableArray *ThatArray = aClass.MyArray;
    
    NSLog("Count of ThatArray: %d", [That.Array count]);
    
    0 讨论(0)
  • 2020-12-22 06:53

    Basically, instead of creating a new view controller, you need to maintain a pointer to the original.

    I suggest storing an instance of your UIViewController in the AppDelegate in order to retain the particular instance of the view controller you've created by making it a global variable.

    ex. In the App Delegate.h

    #import "ViewController.h"
    @class ViewController;
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (nonatomic) ViewController *viewController;
    

    Then from whatever view controllers' .m's from which you need to read/write to the variable, create a pointer to the application's app delegate, ex:

    #import "AppDelegate.h"
    
    @interface WhateverViewController ()
    
    AppDelegate *mainDelegate;
    
    - (void)viewDidLoad {
    
        mainDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    }
    

    So wherever you first create that view controller in your code (before ever using it), initialize it using this global variable. ex. If you're using xibs:

    mainDelegate.viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [self.navigationController pushViewController:mainDelegate.viewController animated:YES];
    

    ex. If you're using storyboards:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
    mainDelegate.viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerID"];
    [self.navigationController pushViewController:mainDelegate.viewController animated:YES];
    

    (This is assuming it's in a place other than the app delegate in which case the pointer to the App Delegate isn't needed.)

    Then when accessing the array from another UIViewController use

    mainDelegate.viewController.array
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 07:00

    What you've done in the code provided is set a public property for a mutable array...

    @property (strong, nonatomic) NSMutableArray *selectedCells;
    

    The NSMutableArray is not "created" by setting that property. At some point in your code you also have to create the NSMutableArray by initialising...

    NSMutableArray *selectedCells = [[NSMutableArray alloc] init];
    

    or by using a convenience method such as...

    NSMutableArray *selectedCells = [NSMutableArray arrayWithCapacity:(NSUInteger)<initialising capacity>];
    

    or

    NSMutableArray *selectedCells = [NSMutableArray arrayWithArray:(NSArray *)<initialising array>];
    

    Initialising an NSMutableArray is often done only once. If it is repeated, the contents are overwritten against the property used to point to the array. As such, a useful location for this is often within the viewDidLoad view controller lifecycle method.

    0 讨论(0)
提交回复
热议问题