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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 02-21-2011, 03:05 PM   #1 (permalink)
Registered Member
 
TheFlyingDutchman's Avatar
 
Join Date: Feb 2011
Location: NYC
Posts: 81
TheFlyingDutchman is on a distinguished road
Default NSTimer

Hi!

Is there a way to pass a variable from class to class? I have an app that has different views (each with its own .m and .h file), and I'd like to pass an integer from one class to another, in this case being an int that an NSTimer increments by 1.


How can I do that?


Thanks,

Dutchman
TheFlyingDutchman is offline   Reply With Quote
Old 02-21-2011, 06:15 PM   #2 (permalink)
Crafty Veteran
 
Magic Hands's Avatar
 
Join Date: Nov 2008
Location: Memphis, TN
Age: 23
Posts: 436
Magic Hands is on a distinguished road
Send a message via Skype™ to Magic Hands
Default

This.
Or this.
Or even this.
__________________
Magic Hands is offline   Reply With Quote
Old 02-21-2011, 07:21 PM   #3 (permalink)
Registered Member
 
TheFlyingDutchman's Avatar
 
Join Date: Feb 2011
Location: NYC
Posts: 81
TheFlyingDutchman is on a distinguished road
Default

Quote:
Originally Posted by Magic Hands View Post
Ha you're funny Magic Hands. But seriously, I'm new to iPhone SDK. How can I pass a variable from one class to another? For example:


Code:
Class A:



-(void)viewDidLoad{


     NSString *myString = @"Hello World";


}



Class B:


#import "ClassA.m"


-(void)viewDidLoad{

//coming from ClassA
NSLog(myString);


}
TheFlyingDutchman is offline   Reply With Quote
Old 02-21-2011, 07:41 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default Use a singleton data object

Quote:
Originally Posted by TheFlyingDutchman View Post
Hi!

Is there a way to pass a variable from class to class? I have an app that has different views (each with its own .m and .h file), and I'd like to pass an integer from one class to another, in this case being an int that an NSTimer increments by 1.


How can I do that?


Thanks,

Dutchman

Look up the singleton design pattern. A singleton data container object works beautifully for sharing data between classes.

A singleton is an object that only gets created once. It has a class method that returns a pointer to itself. The implementation of the class method might look like this:


Code:
+(FooAppDataObject*) myDataObject;
{
  static FooAppDataObject* _myDataObject = nil;
  
  if (_myDataObject == nil)
    _myDataObject = [[FooAppDataObject alloc] init;
  return _myDataObject;
}
Then you add properties to your FooAppDataObject for any values you want to share between classes in your app:

Code:
@interface FooAppDataObject
{
int timerCount;
}
@property (nonatomic, assign) timerCount;

Then, any time you want to refer to a property of your data object, just use code like this:

Code:
[FooDataObject myDataObject].timerCount = [FooDataObject myDataObject].timerCount + 1;

There are lots of examples of singletons in Cocoa: Apple typically calls the class method to return a singleton shared<classname>.

Examples: NSUserDefaults sharedUserDefaults, NSFileManager sharedDataManager, etc, etc.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-21-2011, 09:03 PM   #5 (permalink)
Registered Member
 
TheFlyingDutchman's Avatar
 
Join Date: Feb 2011
Location: NYC
Posts: 81
TheFlyingDutchman is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Look up the singleton design pattern. A singleton data container object works beautifully for sharing data between classes.

A singleton is an object that only gets created once. It has a class method that returns a pointer to itself. The implementation of the class method might look like this:


Code:
+(FooAppDataObject*) myDataObject;
{
  static FooAppDataObject* _myDataObject = nil;
  
  if (_myDataObject == nil)
    _myDataObject = [[FooAppDataObject alloc] init;
  return _myDataObject;
}
Then you add properties to your FooAppDataObject for any values you want to share between classes in your app:

Code:
@interface FooAppDataObject
{
int timerCount;
}
@property (nonatomic, assign) timerCount;

Then, any time you want to refer to a property of your data object, just use code like this:

Code:
[FooDataObject myDataObject].timerCount = [FooDataObject myDataObject].timerCount + 1;

There are lots of examples of singletons in Cocoa: Apple typically calls the class method to return a singleton shared<classname>.

Examples: NSUserDefaults sharedUserDefaults, NSFileManager sharedDataManager, etc, etc.
Thanks, but this seems way too hard. Is there an easier way? BTW, I don't get the point of using @property. What are they? You seem like a good mentor. Please help me!


Also some other unrelated questions:

What's the point of the #define ?

What does the _ifdef thing do?

In english, what is the + operator for the class method? I have no idea what that means.


Thanks,

Dutchman
TheFlyingDutchman is offline   Reply With Quote
Old 02-22-2011, 06:44 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by TheFlyingDutchman View Post
Thanks, but this seems way too hard. Is there an easier way? BTW, I don't get the point of using @property. What are they? You seem like a good mentor. Please help me!


Also some other unrelated questions:

What's the point of the #define ?

What does the _ifdef thing do?

In english, what is the + operator for the class method? I have no idea what that means.


Thanks,

Dutchman
Sigh.

It sounds like you don't know anything at all about Objective C. It's not practical to teach it to you in a forum. You need to buy a good book on Objective C and do some self-study. There are also some good online guides.

Here's a good intro to the concepts:

Loading…

And the Objective C reference: http://developer.apple.com/library/m...tiveC/ObjC.pdf
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-20-2011, 01:56 PM   #7 (permalink)
Registered Member
 
TheFlyingDutchman's Avatar
 
Join Date: Feb 2011
Location: NYC
Posts: 81
TheFlyingDutchman is on a distinguished road
Default

Alright. Now coming back to what I posted before, I realize I was a complete N00B. Anyways, one question: If I have a method, lets say burp:

Code:
-(int)burp:(int)times{
 
     //have some timer

     return times;
}
Can that variable that is returned be constantly updated? Like if I have a timer and I want to increment an integer every second, in my secondary class, can that instance variable be constantly changed? Or once the instance is called, then the variable is static?

Complete N00B question.
TheFlyingDutchman is offline   Reply With Quote
Reply

Bookmarks

Tags
nstimer, passing, variables

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
» Online Users: 381
15 members and 366 guests
Absentia, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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