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 01-26-2012, 08:04 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
dkun7944 is on a distinguished road
Default Setting text from label as an integer

Hello. I am very new to iPhone development so please excuse me if I don't use correct terminology. I am creating one of my first apps. It is a basic counter that increases or decreases the number on a label when you press the increase or decrease buttons. There is also a text field and a button that when pressed, changes the label to the text in the text field. But after the text field text is changed, when the increase button is pressed, the number doesn't increase from the number I entered in the text field. It increases from the number it last left off on. I want the number to increase from the number I entered in the text field. I know it sounds confusing but please try to help me. Here is some of my code from the h file:

Code:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UILabel * label;
    IBOutlet UITextField * textField;
    int inum;
    } 
- (IBAction)increasebuttonPress:(id)sender;
- (IBAction)decreasebuttonPress:(id)sender;
- (IBAction)settextbuttonPress:(id)sender;

@end
and from the m file:
Code:
#import "ViewController.h"

@implementation ViewController

- (IBAction)increasebuttonPress:(id)sender; {
    inum = inum + 1;
    [label setText:[NSString stringWithFormat:@"%d", inum]];
}
- (IBAction)decreasebuttonPress:(id)sender; {
    inum = inum - 1;
    [label setText:[NSString stringWithFormat:@"%d", inum]];
}
- (IBAction)settextbuttonPress:(id)sender; {
    if ([[textField text] floatValue] < 0) {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"You can only enter a number greater than 0."
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
    }
    else
    {
        [label setText:[textField text]];
    }
}
dkun7944 is offline   Reply With Quote
Old 01-26-2012, 08:29 PM   #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 dkun7944 View Post
Hello. I am very new to iPhone development so please excuse me if I don't use correct terminology. I am creating one of my first apps. It is a basic counter that increases or decreases the number on a label when you press the increase or decrease buttons. There is also a text field and a button that when pressed, changes the label to the text in the text field. But after the text field text is changed, when the increase button is pressed, the number doesn't increase from the number I entered in the text field. It increases from the number it last left off on. I want the number to increase from the number I entered in the text field. I know it sounds confusing but please try to help me. Here is some of my code from the h file:

Code:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UILabel * label;
    IBOutlet UITextField * textField;
    int inum;
    } 
- (IBAction)increasebuttonPress:(id)sender;
- (IBAction)decreasebuttonPress:(id)sender;
- (IBAction)settextbuttonPress:(id)sender;

@end
and from the m file:
Code:
#import "ViewController.h"

@implementation ViewController

- (IBAction)increasebuttonPress:(id)sender; {
    inum = inum + 1;
    [label setText:[NSString stringWithFormat:@"%d", inum]];
}
- (IBAction)decreasebuttonPress:(id)sender; {
    inum = inum - 1;
    [label setText:[NSString stringWithFormat:@"%d", inum]];
}
- (IBAction)settextbuttonPress:(id)sender; {
    if ([[textField text] intValue] <= 0) {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"You can only enter a number greater than 0."
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];
    }
    else
    {
        [label setText:[textField text]];
        iNum = [[textField text] intValue];
    }
}
A couple of things. If you want to require the user to enter a value greater than zero, you should reject anything <= 0, not just <0. Your code as written will allow zero values.

Since your value is an integer, you should probably take the intValue of the text field rather than the float value.

Next, take the textField text and assign it's int value to iNum (changes shown in red)
__________________
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 01-27-2012, 06:02 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
dkun7944 is on a distinguished road
Default

Thanks a lot. That worked. Also, is there any way to detect if the text you entered in the text field is a number or a letter?
dkun7944 is offline   Reply With Quote
Reply

Bookmarks

Tags
button, integer, label, text, textfield

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: 403
9 members and 394 guests
Atatator, condor304, FrankWeller, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

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