using UItextfield for numeric input

Discussion in 'iOS Programming' started by just66horns, Feb 2, 2010.

  1. just66horns macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #1
    Im having trouble with taking the typed numbers in a UItextfield, converting them to a float, and using them in a simple math method.
    can anyone provide the code to take two UI textfeilds and, for example, add the values together...My math is a little more complex, but I have that part down.
    Thanks in advance..
    A link would be fine if easier...
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    can't you use double d = [myUITextBox doubleValue]; ?
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    First, you'll want to make sure you using the right class names unless you want to confuse everybody. There is no UItextbox. I'm guessing you mean UITextField, although there is UITextView as well.

    Second, you'll want to pull the text out of your UITextFields. There's a property for that. Then, with those NSStrings, you'll want to get their values as float. NSString has an instance method for that. Hope that helps.
     
  4. just66horns thread starter macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #4
    using "UITextFeild" for numeric input

    Yes, I meant UITextFeild, as you state. I went and fixed the original thread..

    I will go and check that out. I think I know my problem. I'm new and confused.
    (I was confusing VB's text box with ObjC...
    I was using TextFeild in the code, but I only said text box here
     
  5. just66horns thread starter macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #5
    but...

    to get the text, I need
    Code:
    @property(nonatomic, copy) NSString *text;
    Is this appropriate for a double, or do I then take the text, and declare it a double, therefore the property statement would be..

    something like
    Code:
    @property(nonatomic, copy) NSnumber *textNumber;
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'm sorry. I'm a little confused at what you're trying to do there. That's how the text property is defined, not how you use it. To you use that property (or assign via the accessor method; which the dot notation does behind the scenes anyways) either
    Code:
    NSString *textValue = aTextField.text;
    or
    Code:
    NSString *textValue = [aTextField text];
     
  7. just66horns thread starter macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #7
    Got it working

    Thanks,
    That got it!
    doubleValue did not work, but floatValue did.
    Excuse my thickness, and thanks for the help. My app is reaching functionality.. But there will be more questions.

    Code:
    	float m = [textFieldM.text floatValue];
    	float g = [textFieldG.text floatValue];
    	float fy = m / g;
    
     

Share This Page