0

I'm trying to access a NSMutableArray from a different already existing class than it was created in. But if i NSLog it, i get null. My program starts up in class2, then I segue to class1, create my NSMutableArray by pressing one or more rows, and then I want my class2 to get the updated NSMutableArray instance, but all it get is null. Code below:

//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]);
}
}
1
  • may be it's because you are trying to view the array elements right after you initialized your FocusTagTableViewController which at this point has an empty array. Commented Apr 21, 2012 at 17:15

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I'm still relative new to objective-c. How would you use Key-Value-Observing for this problem?
I upgraded my code, but now i just get an error message: An instance 0x7e7e300 of class FocusTagTableViewController was deallocated while key value observers were still registered with it.
Than you have to retain it,when you move to another view controller
I happens as soon as i begin the app though, i never even change that view controller. Also i tried changing allSelectedFocus to retain, but it didn't help either. I never call RemoveObserver, could that be causing the problem?
Maybe,because than you may be observing it twice
|
0

Try this way:

1) Import your class2.h file in your class1 file.

2) Create an NSMutableArray *foo globally in your class2.h file

3) create an object for class2 in your class1 file

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

1 Comment

I would prefer not to make any global variables, so sadly that solution wouldn't work. Thanks though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.