If I have 2 .h and .m files and I want to pass the value of a variable between them, how would I do it?
So in Slide1.m it declares Variable1 = 10; then I want Slide2.m to be able to declare Variable2 = Variable1; but it obviously says "Variable1 undeclared (first use in a function)"
You can set up variable1 as a property of the Slide1 class. Look up properties in your favorite cocoa reference.
Then if you have variable1 as a property of Slide1, you can say variable2 = slide1.variable1.
PS it's typical to use uppercase letters to start class names (Slide1, Slide2) and lowercase for variables (variable1, variable2, slide1 which is an instance of Slide1). It makes it easier for someone to understand what's a class and what's a variable in your program.
Thanks so much. I'm searching for it now but I can't find it. Is there anywhere in particular you would recommend?
No, not a property list - you need a class property, with @property and @synthesize. It's an easy way to get and set variables in one class from another class.
The problem is when I do something like
"MyView.str = @"Testing this";"
I get "syntax error before . token" unless I'm missing something here.
Is MyView a class, or an instance (object) ? The dot.syntax only works on an instance. If myView is an instance of MyViewClass, and MyViewClass has a property of .str, then myView.str= @"Testing this"; should work.
If I have 2 .h and .m files and I want to pass the value of a variable between them, how would I do it?
So in Slide1.m it declares Variable1 = 10; then I want Slide2.m to be able to declare Variable2 = Variable1; but it obviously says "Variable1 undeclared (first use in a function)"
Thanks in advance
If Variable1 and Variable2 are integers, then they don't have to be instance variables of any class. You can simply declare them both to be global variables. In Slide1.h, use:
Code:
extern int Variable1;
@interface Slide1
. . .
@end
and similarly for Slide2.h. And in the implementation files, use:
Code:
int Variable1;
@implementation Slide1
. . .
@end
You see that the declaration and the definition are outside of the @interface and @implementation blocks. It is up to you to ensure that there is only one instance of the Slide1 and Slide2 classes.
The problem with using instance variables, even if they are properties, is that you need to also have a reference to the instance of the class they belong to. How does Slide1.m have any pointer to an instance of Slide2? There could potentially be several Slide1 instances and several Slide2 instances. But from your description, it appears that you are thinking of just one instance of each class.
Is MyView a class, or an instance (object) ? The dot.syntax only works on an instance. If myView is an instance of MyViewClass, and MyViewClass has a property of .str, then myView.str= @"Testing this"; should work.
In TestAppDelegate.h I've put
Quote:
TestTrack *viewController2;
and
@property (nonatomic, retain) IBOutlet TestTrack *viewController2;
In the right places
So when I say "viewController2.str = @"Testing this";" it should work? Unless I'm doing something terribly wrong.
Quote:
Originally Posted by RLScott
If Variable1 and Variable2 are integers, then they don't have to be instance variables of any class. You can simply declare them both to be global variables. In Slide1.h, use:
Code:
extern int Variable1;
@interface Slide1
. . .
@end
and similarly for Slide2.h. And in the implementation files, use:
Code:
int Variable1;
@implementation Slide1
. . .
@end
You see that the declaration and the definition are outside of the @interface and @implementation blocks. It is up to you to ensure that there is only one instance of the Slide1 and Slide2 classes.
The problem with using instance variables, even if they are properties, is that you need to also have a reference to the instance of the class they belong to. How does Slide1.m have any pointer to an instance of Slide2? There could potentially be several Slide1 instances and several Slide2 instances. But from your description, it appears that you are thinking of just one instance of each class.
Robert Scott
Ypsilanti, Michigan
They are "Float32's". When I try that it simply returns "null", is there anything else I have to do to get it working?
When you try what? What returns NULL? What do you want to get working? You have got to be more clear if you expect useful help.
Robert Scott
Ypsilanti, Michigan
Ok, sorry . If I've followed what you've said and then set variable1 to 10 in slide1 then in slide2 said variable2 = variable1 then when I write variable2 out to a label it displays it as "NULL". If that makes sense?
Ok, sorry . If I've followed what you've said and then set variable1 to 10 in slide1 then in slide2 said variable2 = variable1 then when I write variable2 out to a label it displays it as "NULL". If that makes sense?
No, it doesn't. What do you mean "write variable2 out to a label"? Integer or float variables cannot be just "written out to a label". They can be converted to a string using the NSString initWithFormat method. Then the resulting string can be assigned to the text property of a label. That is the closest I can come to "writing out variable2 to a label".
No, it doesn't. What do you mean "write variable2 out to a label"? Integer or float variables cannot be just "written out to a label". They can be converted to a string using the NSString initWithFormat method. Then the resulting string can be assigned to the text property of a label. That is the closest I can come to "writing out variable2 to a label".
Robert Scott
Ypsilanti, Michigan
By "writing out to a label" I meant the whole process of writing it out to a label such as creating a new NSString and using "initWithFormat" to put it in and then declaring the label as the new NSString. As you can probably tell I'm new to Objective-C, although I do know some other languages, but once I've sorted this then I'm pretty much set.
Then what do you mean by "displays as NULL"? Do you mean that it shows the 4 letter word "NULL" in the label? Or do you mean the contents of the label appears blank? Whichever it is, this has nothing to do with your original question about passing variables between two classes. If you are still having trouble writing a value out to a label, then post all the relevant code.