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 05-21-2011, 11:02 AM   #1 (permalink)
iPhone Developer
 
alex5776's Avatar
 
Join Date: May 2011
Location: Minnesota
Age: 14
Posts: 26
alex5776 is on a distinguished road
Question Number Textbox

Hey,
How can I get my app to add and subtract based on a button pressed.
I have a text field with two buttons next to it. When I tap the top one i want the text field to display 1, tap again 2, as high as the user wants it to be.
The second buttons does it the opposite, tab it will go down, but I don't want it to go into negatives.

I have done NO math type things in iPhone development so I don't even know where to start.

Any help?
__________________
-----------
Want to see me run to the apple store and back?
~~~Moments Later~~~
Want to see me do it again?
alex5776 is offline   Reply With Quote
Old 05-21-2011, 11:18 AM   #2 (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 alex5776 View Post
Hey,
How can I get my app to add and subtract based on a button pressed.
I have a text field with two buttons next to it. When I tap the top one i want the text field to display 1, tap again 2, as high as the user wants it to be.
The second buttons does it the opposite, tab it will go down, but I don't want it to go into negatives.

I have done NO math type things in iPhone development so I don't even know where to start.

Any help?

I would suggest using an integer or floating point instance variable in your view controller to hold the current value you display.

Set that instance variable up as a property with a custom setter.

In the setter for your value, display the new value to your text field. Something like this:


The header for your view controller:
Code:
@interface myViewController: UIViewController
{
NSInteger intValue;
IBOutlet UILabel* valueDisplay;
}

@property (nonatomic, assign) NSInteger intValue;
@end

The .m file for your view controller:
Code:
@implementation myViewController

@synthesize intValue;

- (void) setIntValue: (NSInteger) newValue;
{
  if (intValue == newValue || intValue < 0)
    return;
  intValue = newValue;

  //The line below takes the new intValue, converts it to a string,
  //and displays it in the valueDisplay UILabel.
  valueDisplay.text = [NSString stringWithFormat: @"%d", intValue];
}

(IBAction) addToButton;
{
  self.intValue =self.intValue + 1;
}

(IBAction) subTractFromButton;
{
  self.intValue =self.intValue - 1;
}


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

Last edited by Duncan C; 05-21-2011 at 11:21 AM.
Duncan C is offline   Reply With Quote
Old 05-21-2011, 11:24 AM   #3 (permalink)
iPhone Developer
 
alex5776's Avatar
 
Join Date: May 2011
Location: Minnesota
Age: 14
Posts: 26
alex5776 is on a distinguished road
Default

Thank you so much.
I should be able to work off of that.
__________________
-----------
Want to see me run to the apple store and back?
~~~Moments Later~~~
Want to see me do it again?
alex5776 is offline   Reply With Quote
Reply

Bookmarks

Tags
add, buttons, math, subtract, textfield load

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: 356
7 members and 349 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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