简体   繁体   English

从另一个类访问NSMutableArray-目标C

[英]Access NSMutableArray from another class - Objective C

I have a main ViewController that contains a desginated class. 我有一个主要的ViewController包含一个目标类。 Within that ViewController there is a Container that is linked to an embed ViewController . 在该ViewController有一个Container链接到embed ViewController Within that embed ViewController I am creating an NSMutableArray . 在那个embed ViewController我正在创建一个NSMutableArray I am not trying to access that array inside the main ViewController . 我没有尝试访问主ViewControllerarray I know that if I use: 我知道如果我使用:

create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];
NSLog(@"%@",myScript.selectedCells);

The NSLog will output null because I am creating a new ViewController and that gets rid of the already set array . NSLog将输出null,因为我正在创建一个新的ViewController ,它会删除已经设置的array So my question is how can I access that array without overwriting it? 所以我的问题是如何在不覆盖该array情况下访问该array

UPDATE: 更新:

Heres where the NSMutableArray is being created: 以下是创建NSMutableArray位置:

create_challenge_peopleSelect.h : create_challenge_peopleSelect.h

@property (strong, nonatomic) NSMutableArray *selectedCells;

create_challenge_peopleSelect.m : create_challenge_peopleSelect.m

    if([selectedCells containsObject:label.text])
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectedCells removeObjectIdenticalTo:label.text];

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectedCells addObject:label.text];
    }

This class is the container class off the main ViewController 此类是主ViewControllercontainer class

No I want to access the selectedCells within my main ViewController , I have been doing things such as: 不,我想访问主ViewControllerselectedCells ,我一直在做如下事情:

create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];

I would prefer to stay away from the App Delegate If possible. 如果可能,我希望远离App Delegate

You seem to be unclear on the difference between classes and instances. 您似乎还不清楚类和实例之间的区别。 OK, so, say we have two NSArrays: 好,所以,说我们有两个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. 如果执行a.count ,我将得到5作为答案,因为该数组包含五个对象。 Meanwhile, if I do b.count , I'll get 3, because that array contains three objects. 同时,如果我执行b.count ,我将得到3,因为该数组包含三个对象。 It isn't that creating b "gets rid of the already set count ". 不是创建b “摆脱已经设置的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. 所以这是情境。

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. 我建议将UIViewController的实例存储在AppDelegate中,以保留通过将其创建为全局变量而创建的视图控制器的特定实例。

ex. 例如 In the App Delegate.h 在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: 然后,从需要从中读取/写入变量的任何视图控制器的.m中,创建指向应用程序的应用程序委托的指针,例如:

#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: 如果您使用的是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 然后,当从另一个UIViewController访问数组时,使用

mainDelegate.viewController.array

To access the NSMutableArray from one class to another class use following code. 要从一个类访问NSMutableArray到另一个类,请使用以下代码。

In the first view controller in which u have declared the object of NSMutableArray , declare the property and synthesize for the same as below, 在第一个您已声明NSMutableArray对象的视图控制器中,声明property并对其进行synthesize ,如下所示:

//In FirstViewcontroller.h class, //在FirstViewcontroller.h类中,

@property (nonatomic, strong) NSMutableArray *arrData;

//In FirstViewcontroller.m class //在FirstViewcontroller.m类中

@synthesize arrData;

Also FirstViewcontroller object should be global so you can create the object of FirstViewcontroller in app delegate file. 另外,FirstViewcontroller对象应该是全局的,因此您可以在应用程序委托文件中创建FirstViewcontroller的对象。

//appdelegate.h //appdelegate.h

@property (nonatomic, strong) FirstViewcontroller *objFirst;

//appdelegate.m //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中,创建Appdelegate文件的共享对象

//SecondViewcontroller.m //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. 这是您所需的数组,希望对您有所帮助。

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. 不能通过设置该属性来“创建” NSMutableArray At some point in your code you also have to create the NSMutableArray by initialising... 在代码的某些时候,您还必须通过初始化来创建NSMutableArray

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. 初始化NSMutableArray通常仅执行一次。 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. 这样,一个有用的位置通常位于viewDidLoad视图控制器生命周期方法中。

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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM