从另一个类访问NSMutableArray-目标C [英] Access NSMutableArray from another class - Objective C

查看:87
本文介绍了从另一个类访问NSMutableArray-目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的ViewController,其中包含一个指定的类.在该ViewController中,有一个Container链接到embed ViewController.在该embed ViewController中,我正在创建一个NSMutableArray.我没有尝试在主ViewController内部访问该array.我知道如果我使用:

I have a main ViewController that contains a desginated class. Within that ViewController there is a Container that is linked to an embed ViewController. Within that embed ViewController I am creating an NSMutableArray. I am not trying to access that array inside the main ViewController. I know that if I use:

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

NSLog将输出null,因为我正在创建一个新的ViewController,并且它摆脱了已经设置的array.所以我的问题是如何在不覆盖array的情况下访问该文件?

The NSLog will output null because I am creating a new ViewController and that gets rid of the already set array. So my question is how can I access that array without overwriting it?

更新:

在其中创建NSMutableArray的位置:

create_challenge_peopleSelect.h:

@property (strong, nonatomic) NSMutableArray *selectedCells;

create_challenge_peopleSelect.m:

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

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

该类是主ViewController

不,我想访问主ViewController中的selectedCells,我一直在执行以下操作:

No I want to access the selectedCells within my main ViewController, I have been doing things such as:

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

如果可能的话,我宁愿远离App Delegate.

I would prefer to stay away from the App Delegate If possible.

推荐答案

基本上,不需要创建新的视图控制器,而是需要维护指向原始视图的指针.

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

我建议将UIViewController的实例存储在AppDelegate中,以保留通过将其创建为全局变量而创建的视图控制器的特定实例.

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.

例如在App Delegate.h

ex. In the App Delegate.h

#import "ViewController.h"
@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic) ViewController *viewController;

然后从需要从中读取/写入变量的任何视图控制器的.m中,创建指向应用程序的应用程序委托的指针,例如:

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

}

因此,无论您在代码中首次创建该视图控制器的位置(在使用之前),都应使用此全局变量对其进行初始化.前任.如果您使用的是xibs:

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.)

然后从另一个UIViewController使用中访问数组时

Then when accessing the array from another UIViewController use

mainDelegate.viewController.array

这篇关于从另一个类访问NSMutableArray-目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆