views:

76

answers:

2

In one classes .h i have

NSMutableArray *rountines;

and in another classes .m i want to do something like this

 [routines addOject:@"Hello];

the other class is an ModalViewController type set up.

So, in essence i'd like the .m file of the MVC to be able to read, and edit and do other things to the ivars i declare in the header.

Cheers,

Sam

edit

Another example, which is similar to what im trying to achieve is like an edit screen.

A: 

you can either do this by making the ivars you want to share globals (in which case they would be ivars of the singleton class or app delegate class) or by passing a reference to the class you want to modify the ivars of as an argument to a method of the ModalViewController class:

@implementation ModalViewController
......
-(void)addObjectToRoutinesFromClass: (MyClass *)myclass {
        [myclass.routines addObject:@"Hello"];
}

@implementation MyClass
  ......
  ModalViewController *myModalViewController = [[ModalViewController alloc] init];  
[myModalViewController addObjectToRoutinesFromClass:self];

@end
ennuikiller
thanks, but how do I do that? :S "newbie" here
Sam Jarman
instance variables aren't global, they belong to an instance of an object.
darren
@darren....objective-c globals are ivars of the class you define them in which I clarified in the answer.
ennuikiller
@Sam see the example code in the answer for how to pass a reference to the class whose ivars you want to share. I've added a link for how to use singleton to contain globals.
ennuikiller
+1  A: 

You can't share ivars between classes really. ivar stands for instance variable, and that is a variable that belongs to some particular instance of an object. The way to solve this problem is to allow other objects to access this object's state. This is most commonly done through setter and getter methods. Objective-C 2.0 makes this easier by providing the @property and @synthesize keywords.

If you had access to the object that had the routines array, you could access it through its property (getter method) like this:

[[someObject routines] addObject:@"hello"];
darren
Also check this out and read about declared properties: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/Introduction/introObjectiveC.html
Nimrod
properties can be accessed like someObject.routines too. So [someobject.routines addObject:@"yello"];
CVertex
would 'someObject' be the class it lived in, or what? Can you please explain that a bit further?
Sam Jarman
close. someObject is an instance of the class that declared the routines array. To elaborate, routines is an instance variable of type NSMutableArray. It belongs to the object someObject. We dont know, from this code snippet, what kind of class the someObject was made from. the statement [someObject routines] sends the routine message to the someObject object. the routine message is an accessor method of someObject that returns the routine instance variable.
darren