2012-11-24 2 views
2

나는이 엑스 코드와 작업을 계산합니다.텍스트 필드 날짜 선택기 계산

오늘은 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

일 수를 계산하므로 float 대신 int을 사용할 수 있습니다.

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

ok. 나는 충돌의 문제가있다. –

+0

로그를 붙여 넣기하고 질문을 업데이트하십시오. – sunkehappy

+0

이 충돌은 스토리 보드의 텍스트 필드였습니다. –