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

私の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];


}

私はこれが間違った方法であることを知っています....私を助けてもらえますか?

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 に答える