English 中文(简体)
查阅其他现有类别的NSMutable Array
原标题:Access NSMutableArray from other existing class

我试图从已经存在的不同类别中获取一个NSMutableArray。 但是,如果说是安全记录仪,那就是无效的。 我的节目以2级开始,然后是I segue到1级,用一个或一个以上的行文来制造我的NSMutableArray,然后我想我的班子2获得最新的NSMutable Array,但所有这些都是无效的。 法典如下:

//class1.m

#import "FocusTagTableViewController.h"
#import "STATableViewController.h"

@implementation FocusTagTableViewController

@synthesize focusArray = _focusArray;
@synthesize allSelectedFocus = _allSelectedFocus;

- (void)viewDidLoad
{
_focusArray = [[NSArray alloc]initWithObjects:@"Balance",@"Bevægelse",@"Elementskift",@"Vejrtrækning",@"Alle",nil];

[super viewDidLoad];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedFocus = [[_focusArray objectAtIndex:indexPath.row] stringByAppendingString:@","];
if(_allSelectedFocus == nil)
{
    _allSelectedFocus = [[NSMutableArray alloc]init];
    [_allSelectedFocus addObject:selectedFocus]; 
}
else if(![_allSelectedFocus containsObject:selectedFocus])
{
    [_allSelectedFocus addObject:selectedFocus];
}
}

//class2.m

#import "STATableViewController.h"
#import "FocusTagTableViewController.h"

@implementation STATableViewController
- (void)viewDidLoad
{
[super viewDidLoad];

 FocusTagTableViewController *focusTag = [[FocusTagTableViewController alloc]init];

[focustag addObserver:self forKeyPath:@"allSelectedFocus" options:NSKeyValueObservingOptionNew context:NULL];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"allSelectedFocus"])
{
    NSLog(@"%@", [object valueForKeyPath:keyPath]);
}
}
最佳回答

That s because in class 2 you create a new instance of class 1, wich has empty array. If you want to access that array from class 2, you should make a reference to that first class. Or you could use Key-Value-Observing for that.

Here is the docs about KVO http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

In first class, when you create class two, add self as an observer for that array, and implement observeValueForKeyPath: method in class 1

问题回答

采用这种方式:

(1) 进口贵类2.h文档。

(2) 在你的类别2.h档案中,在全球范围创建NSMutableArray *foo

3) 在您的类别1 档案中制造2级物体

4) instead Of using allSelectedFocus in your class1 file, set objects to foo in that file itself like this [class2Object.foo addObject:selectedFocus];





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签