Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-13-2009, 04:31 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default Passing variables

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
ozzie is offline   Reply With Quote
Old 06-13-2009, 05:54 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-13-2009, 06:06 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default

Thanks so much. I'm searching for it now but I can't find it. Is there anywhere in particular you would recommend?

Last edited by ozzie; 06-13-2009 at 06:36 PM.
ozzie is offline   Reply With Quote
Old 06-13-2009, 06:39 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by ozzie View Post
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.

Try these, if your book doesn't have it. They're a new feature in Objective C 2.0
Theocacao: A Quick Objective-C 2.0 Tutorial
Mac Developer Tips Objective-C: Properties, Setters and Dot Syntax
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-13-2009, 07:00 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default

The problem is when I do something like
"MyView.str = @"Testing this";"

I get "syntax error before . token" unless Im missing something here.
ozzie is offline   Reply With Quote
Old 06-13-2009, 07:39 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by ozzie View Post
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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-13-2009, 08:03 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

Quote:
Originally Posted by ozzie View Post
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.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 06-14-2009, 04:00 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default

Quote:
Originally Posted by smasher View Post
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
In TestAppDelegate.m I've put
Quote:
TestTrack *aViewController = [[TestTrack alloc]
initWithNibName:@"ControllerView" bundle:[NSBundle mainBundle]];
self.viewController2 = aViewController;
[aViewController release];
So when I say "viewController2.str = @"Testing this";" it should work? Unless I'm doing something terribly wrong.

Quote:
Originally Posted by RLScott View Post
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?

Last edited by ozzie; 06-14-2009 at 04:38 PM.
ozzie is offline   Reply With Quote
Old 06-14-2009, 05:22 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

Quote:
Originally Posted by ozzie View Post
...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
RLScott is offline   Reply With Quote
Old 06-14-2009, 05:37 PM   #10 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default

Quote:
Originally Posted by RLScott View Post
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?
ozzie is offline   Reply With Quote
Old 06-14-2009, 06:11 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

Quote:
Originally Posted by ozzie View Post
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".

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 06-15-2009, 06:41 AM   #12 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 140
Default

Quote:
Originally Posted by RLScott View Post
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.
ozzie is offline   Reply With Quote
Old 06-15-2009, 10:37 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

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.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 157,862
Threads: 88,915
Posts: 379,298
Top Poster: BrianSlick (7,072)
Welcome to our newest member, Pablo70
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:58 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0