2

我想用 Xcode 计算这个操作:

A*B*(天)=R

DAYS=从过去的日期选择器到今天有多少天。

今天是 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

我的

- (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];


}

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

4

1 回答 1

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];
于 2012-11-24T04:44:29.103 回答