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 12-10-2011, 06:34 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 52
tekky is on a distinguished road
Default How to call a function when an integer changes WITHOUT using an NSTimer

I was wondering if there was a way to call a function when an integer goes below 1 or changes at all.

Normally I would do this:

Code:
blah.h
int playerHP;
NSTimer *timer;



blah.m
-(void)update
{
    if (playerHP < 1) {
        [timer invalidate];
        [self deathanimation];
    }
}

- (void)viewDidLoad
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(update) userInfo:nil repeats:YES];

}
Is there a way to call the "update" function WITHOUT using an NSTimer? I just don't want to make like 100 timers to be watching certain integers...

ORRR... is there even a way to just check constantly if the integer changes at all and then call a function? Like for example... if a character gets gold in a game... then it sees that the gold integer changes and it automatically calls a function to save it!

I would really appreciate if anyone could help.

Thank you in advance.

Last edited by tekky; 12-10-2011 at 06:42 AM.
tekky is offline   Reply With Quote
Old 12-10-2011, 07:20 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Create a property for that integer with a custom setter method, in that setter you can check the value and do whatever you want to "on value change". Then in your class, when using the integer use the property/setter instead of setting its value directly.
baja_yu is offline   Reply With Quote
Old 12-10-2011, 09:40 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 52
tekky is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Create a property for that integer with a custom setter method, in that setter you can check the value and do whatever you want to "on value change". Then in your class, when using the integer use the property/setter instead of setting its value directly.
Have an example or any sites on how to make a custom setter method? Not sure I've done that before.
tekky is offline   Reply With Quote
Old 12-10-2011, 11:12 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 280
dapis is on a distinguished road
Default

Quote:
Originally Posted by tekky View Post
I was wondering if there was a way to call a function when an integer goes below 1 or changes at all.

Normally I would do this:

Code:
blah.h
int playerHP;
NSTimer *timer;



blah.m
-(void)update
{
    if (playerHP < 1) {
        [timer invalidate];
        [self deathanimation];
    }
}

- (void)viewDidLoad
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(update) userInfo:nil repeats:YES];

}
Is there a way to call the "update" function WITHOUT using an NSTimer? I just don't want to make like 100 timers to be watching certain integers...

ORRR... is there even a way to just check constantly if the integer changes at all and then call a function? Like for example... if a character gets gold in a game... then it sees that the gold integer changes and it automatically calls a function to save it!

I would really appreciate if anyone could help.

Thank you in advance.
Why not just use a single timer and in its selector method check for changes to all the variables in question? I'm not sure why you'd want different timers for different variables.
dapis is offline   Reply With Quote
Old 12-11-2011, 04:06 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 52
tekky is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Create a property for that integer with a custom setter method, in that setter you can check the value and do whatever you want to "on value change". Then in your class, when using the integer use the property/setter instead of setting its value directly.

What is this "on value change" command?
tekky is offline   Reply With Quote
Old 12-11-2011, 07:44 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 33
X-RaY is on a distinguished road
Default

I guess KVO would be sufficient

small example:

Code:
@interface myModel : NSObject {
NSInteger funnyInt;
}
-(void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
-(void)removeObserver:(NSObject *)observer forKeyPath:(NSString *);
Implementation:
Code:
-(void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
	[super addObserver:observer forKeyPath:keyPath options:options context:context];
}
-(void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath{
	[super removeObserver:observer forKeyPath:keyPath];
}
Code:
@interface myObserver : NSObject {
}
-(id)initWithMyModel:(myModel*)mm
-(void)myMethod:(NSInteger)x;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
implementation:
Code:
-(id)initWithMyModel:(myModel*)mm{
if(self=[super init]){
[mm addObserver:self forKeyPath:@"funnyInt" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
	if(object.funnyInt<=1){
		[self myMethod:object.funnyInt];
	}
}

-(void)myMethod:(NSInteger)x{
NSLog("funnyint changed: %d", x);
}
by adding 'myObserver' as observer of the attribute 'funnyInt' of the class 'myModel' via the method:
Code:
addObserver:self forKeyPath:@"funnyInt" options:NSKeyValueObservingOptionNew context:nil;
automatically calls the method
Code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
when funnyInt changes via the method:
[mm setValue:10 forKey:"funnyInt"];

experiment a bit with it, and you'll get the hang of it
X-RaY 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
» Online Users: 412
13 members and 399 guests
7twenty7, AppsBlogger, David-T, Duncan C, EvilElf, HemiMG, heshiming, iekei, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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