文本字段Datepicker计算

时间:2012-11-24 02:06:48

标签: objective-c ios

我想用Xcode计算这个操作:

A * B *(天)= R

DAYS =从过去的datepicker到今天的天数。

今天是iPhone的日期设置。例如:

5 * 5 * 10

10 =昨天和今天之间的天数。

这是我的情况:

我的H.     @interface ViewController:UIViewController

@property (strong, nonatomic) IBOutlet UITextField *a;
@property (strong, nonatomic) IBOutlet UITextField *b;
@property (strong, nonatomic) IBOutlet UILabel *r;

@property (strong, nonatomic) IBOutlet UIButton *result;

@property (strong, nonatomic) IBOutlet UIDatePicker *data;


@end

我的M

- (IBAction)calculate:(id)sender {
    NSDate *past = _data.date ;
    NSDate *now =[NSString stringWithFormat:@"%@",nil];

    **float z = HOW MANY DAYS BETWEEN PICKER AND TODAY;** //this is a example

    float a = ([a.text floatValue]);
    float b = a*([b.text floatValue]);
    float r=  a*b*(z.text float);

    _result.text = [[NSString alloc]initWithFormat:@"%2.f",b];


}

我知道这是错误的方法....你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您计算的天数,因此您可以使用int代替float

NSDate *past = _data.date ;
NSDate *now = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                            fromDate:past
                                              toDate:now
                                             options:0];
z = [components day];

int a = a.text.intValue;
int b = a * b.text.intValue;
int r = a * b * z;
_result.text = [[NSString alloc] initWithFormat:@"%d", r];