将UITextField转换为float

时间:2014-01-23 19:31:50

标签: objective-c uitextfield

您好我正在尝试将用户输入从UITextField设置为Decimal Pad,以使浮点数等于该数量。

//
//  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

在实施文件中,我尝试过以下操作:

- (void)viewDidLoad
{

    tipAmount1 = [tip1 floatValue];

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

但它告诉我“'UITextField'没有可见的@interface声明选择器'floatValue'。任何想法?我试图最终成功,所以我可以有多个UITextFields将输入转换为浮点数,然后添加所有花车总数。

2 个答案:

答案 0 :(得分:1)

tipAmount1 = [[tip1 text] floatValue]; UITextField不仅仅是NSString。它有一个文本,您可以在其中放置字符串。

答案 1 :(得分:1)

- (void)viewDidLoad {
    tipAmount1 = [tip1.text floatValue];

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