Mark Worsnop
Mark Worsnop

Reputation: 4467

setting a var from another class

In Class1 I have a var named "cDte1". I would like to set it from Class2.

Class1.h:

@interface Class1 : UIViewController {

    NSString *cDte1;

}

@property (readwrite,assign) NSString *cDte1;

@end

In Class2.m I imported the Class1.h

I then tried this from Class2

  Class1.cDte1 = @"test";

but that doesn't work. What did I do wrong?

EDIT, more info. If I need to set the var and then show the view the code below doesnt work as I assume there are 2 difference instances of the class?

Class1 *obj = [[Class1 alloc] init];

Class1 *myView = [[Class1 alloc] initWithNibName:@"Class1" bundle:[NSBundle mainBundle]];

obj.cDte1 = @"7/25/2011";


[self presentModalViewController:myView animated:YES];


[myView release];


[obj release];

Upvotes: 0

Views: 63

Answers (4)

Christian Schnorr
Christian Schnorr

Reputation: 10786

Class1 *viewController = [[Class1 alloc] initWithNibName:@"Class1" bundle:[NSBundle mainBundle]];
viewController.cDte1 = @"7/25/2011";

[self presentModalViewController:viewController animated:YES];

[viewController release];

This should work if the ViewController you'd like to push is an instance of Class1. Make sure to have a corresponding InterfaceBuilder file (Class1.xib).
If you create your views manually (without IB, no .xib) don't call initWithNibName:bundle: but just init.

Upvotes: 0

yan
yan

Reputation: 20992

You set member variables on instances of classes, not classes.

So you can do something like:

Class1 obj = [[Class1 alloc] init];
obj.cDte1 = @"test";

edit: in your example, try:

Class1 *myView = [[Class1 alloc] initWithNibName:@"Class1" bundle:[NSBundle mainBundle]];
myView.cDte1 = @"7/25/2011";
[self presentModalViewController:myView animated:YES];
[myView release];

Upvotes: 1

Legolas
Legolas

Reputation: 12345

Create an object from Class1 and then try to access it.

You cannot access properties with class names.

How about

Class1 *object1 = [[Class1 alloc] init];
object1.cDte1 = @"Test";

Upvotes: 0

MByD
MByD

Reputation: 137412

You need to call it on an instance of the class, not the class itself. Something like:

Class1 *instance = [[Class1 alloc] init];
instance.cDte1 = @"test";

Upvotes: 1

Related Questions