2012-11-24 36 views
2

我要計算在Xcode此操作:文本字段日期選擇器計算

A * B *(天)= R

天=多少天是從過去的日期選擇器到今天。

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

您正在計算天數,因此您只能使用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]; 
+0

好的。我有一個崩潰的問題。 –

+0

粘貼你崩潰日誌並更新你的問題。 – sunkehappy

+0

這次崩潰是故事板中的一個文本框 –