Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

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:

#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:
#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]];
}
}
Post edited by dkun7944 on

Replies

  • Duncan CDuncan C Posts: 8,033Tutorial Authors, Registered Users
    dkun7944;404696 said:
    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:

    #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:
    #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] [COLOR=\"red\"][B]intValue[/B][/COLOR]] [COLOR=\"red\"][B]<=[/B][/COLOR] 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]];
    [COLOR=\"Red\"]iNum = [[textField text] intValue];[/COLOR]
    }
    }
    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.<br />
    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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • dkun7944dkun7944 Posts: 2New Users
    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?
Sign In or Register to comment.