-2

Hello I am trying to set the user input from a UITextField set to Decimal Pad to have a float equal to that amount.

//
//  ViewController.h
//  tiptest
//
//  Created by NUGZ on 1/23/14.
//  Copyright (c) 2014 NUGZ. All rights reserved.
//

#import <UIKit/UIKit.h>

float tipAmount1;
float tipAmount2;
float tipAmount3;
float tipAmount4;

@interface ViewController : UIViewController {

IBOutlet UITextField *tip1;
IBOutlet UITextField *tip2;
IBOutlet UITextField *tip3;
IBOutlet UITextField *tip4;

IBOutlet UILabel *total;

}

-(IBAction)tally:(id)sender;

@end

In the implementation file I have tried doing the following:

- (void)viewDidLoad
{

    tipAmount1 = [tip1 floatValue];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

But it is telling me "No visible @interface for 'UITextField' declares the selector 'floatValue'. Any ideas? I am trying to eventually make it so I can have multiple UITextFields that take the input into floats and then add all the floats for a total.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
stickynugz
  • 41
  • 7
  • 1
    Do not use `floatValue` to convert user-entered text to a `float`. It won't work for many users. Use an `NSNumberFormatter` instead. – rmaddy Jan 23 '14 at 20:01

2 Answers2

1

tipAmount1 = [[tip1 text] floatValue]; An UITextField isn't just a NSString. It has a text where you can put the string.

Larme
  • 24,190
  • 6
  • 51
  • 81
1
- (void)viewDidLoad {
    tipAmount1 = [tip1.text floatValue];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Giuseppe Mosca
  • 1,323
  • 12
  • 18
  • Using this method it isn't returning the value inserted into the UITextField, just 0.000000 when the tally button method is called. -(IBAction)tally:(id)sender { total.text = [NSString stringWithFormat:@"%f", tipAmount1]; } – stickynugz Jan 23 '14 at 20:50