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 10-16-2010, 05:14 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 38
loddy1234 is on a distinguished road
Red face Change integer value

This is probably a really stupid question but :
I am making an app and I need to change the value of an integer by +1 every time the button is pressed. When I define the integer, do I need to do anything different than I would when declaring an integer that's value doesn't change? Also, what code would I use to add 1 to the integer each time the button is pressed?
Example code appreciated as I am new to objective C
Thanks in advance
loddy1234 is offline   Reply With Quote
Old 10-16-2010, 05:24 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Code:
NSInteger temp = 0;
And:

Code:
temp++;

Last edited by thomashw; 10-17-2010 at 02:47 AM.
thomashw is offline   Reply With Quote
Old 10-16-2010, 05:30 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 38
loddy1234 is on a distinguished road
Default

Quote:
Originally Posted by thomashw View Post
Code:
NSInteger temp = 0;
And:

Code:
NSInteger++;

Thanks for the speedy reply
loddy1234 is offline   Reply With Quote
Old 10-17-2010, 01:31 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Quote:
Originally Posted by thomashw View Post
Code:
NSInteger temp = 0;
And:

Code:
NSInteger++;
Whoa whoa whoa--huh? NSInteger++ doesn't make any sense. ++ IS the increment operator, but it can't be used after NSInteger because NSInteger is a variable type. It'd have to be something more like:

Code:
NSInteger temp;
temp++;
I prefer to use the normal "int" variable type, but to each his own, I guess.

And for your other question--no, you don't have to declare it in any different way than a variable that doesn't change. There are things called constants which can't change, ever--but declaring those is generally much different than just declaring a normal variable.

I'd look at some beginner's resources, starting, actually, with C--as Objective C is just a strict superset (that is, Objective C is like C plus other stuff). So you might consider picking up a cheap book on basic C, it will probably help you a lot.
__________________
HEY! Was this post helpful?
If so, it would be MUCH appreciated if you'd just click on one of these apps:



MyD
Take 1 minute to set up your MyD and you'll always be able to prove you own your device!

Membrik
Test your memory by sliding tiles to match chains of increasing difficulty.

©2011 Dardom Productions | Like us on Facebook!
SoulRed12 is offline   Reply With Quote
Old 10-17-2010, 02:46 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
Whoa whoa whoa--huh? NSInteger++ doesn't make any sense.
Woops, I was sleepy. Obviously temp++.
thomashw is offline   Reply With Quote
Old 10-17-2010, 06:15 AM   #6 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 38
loddy1234 is on a distinguished road
Unhappy

I tried it using this code
-(IBAction) greenUp: (id) sender{
NSInteger greenValue = 0;
greenValue ++;
green.text = [NSString stringWithFormat:@"%d", greenValue];

}

green is a UITextField

now it just changes the text in the box to 1 and when the button is pressed again, it stays as 1?
any ideas why?
loddy1234 is offline   Reply With Quote
Old 10-17-2010, 09:37 AM   #7 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by loddy1234 View Post
I tried it using this code
-(IBAction) greenUp: (id) sender{
NSInteger greenValue = 0;
greenValue ++;
green.text = [NSString stringWithFormat:@"%d", greenValue];

}

green is a UITextField

now it just changes the text in the box to 1 and when the button is pressed again, it stays as 1?
any ideas why?

You keep resetting it to ZERO and then incrementing it by ONE....that's why it only shows ONE.

Place the equals to ZERO outside the IBAction or...use an IF statement to handle it correctly.
Not_Appy_At_All is offline   Reply With Quote
Old 10-17-2010, 09:57 AM   #8 (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 loddy1234 View Post
I tried it using this code
-(IBAction) greenUp: (id) sender{
NSInteger greenValue = 0;
greenValue ++;
green.text = [NSString stringWithFormat:@"%d", greenValue];

}

green is a UITextField

now it just changes the text in the box to 1 and when the button is pressed again, it stays as 1?
any ideas why?

You need to make greenValue an instance variable. Assign it a value of zero in your object's init method, and then increment it in your IBAction. Something like this:


In your .h file



@interface My_class : NSObject
{
//Delcare instance variables here
NSInteger greenValue;
}

//declare public methods here
@end


In your .m file:



Code:
@implementation My_class

- (void) doSetup;
{
  //set initial value of greenValue
  greenValue = 0;
}

- (id) init;
{
  self = [super init];
  if (!self) return nil;
  [self doSetup];
}

- (id) initWithCoder:(NSCoder *)aDecoder;
{
  self = [super initWithCoder: aDecoder]
  if (!self) return nil;
  [self doSetup];
  return self;
}

@end

Note that for objects that are created from a nib file, init does not get called. The system creates those objects with the method initWithCoder instead. Thus, I usually put my setup code in a separate method doSetup, and call that method from both init and initWithCoder. That way, the same setup gets done if you create the objects in IB or with alloc/init.
__________________
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
Reply

Bookmarks

Tags
button, change, code, integer, pressed

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: 338
8 members and 330 guests
Desert Diva, dre, hain, HemiMG, mottdog, oceanlablight, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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