我有2个日期选择器和1个按钮。当我从选择器中选择日期时,第一个选择器取第二个选择器值。我从第一个选择器选择19.05.2013,从第二个选择器选择20.05.2013但是输出像;
输出日志
< dt1> 20.05.2013< / dt1>第一个选择器
< dt2> 20.05.2013< / dt2>第二个选择器
·H
-(IBAction)Send:(UIButton *)sender;
@property (nonatomic, retain) IBOutlet UITextField *Date1;
@property (nonatomic, retain) IBOutlet UITextField *Date2;
的.m
-(IBAction)Send:(UIButton *)sender{
NSString *mensagemSOAP= [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<UrunToplamiGetir xmlns=\"http://tempuri.org/\">\n"
"<dt1>%@</dt1>\n"
"<dt2>%@</dt2>\n"
"</UrunToplamiGetir>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",Date1.text,Date2.text];
NSLog(@"SOAP Message= \n%@\n\n", mensagemSOAP);
NSURL *url = [NSURL URLWithString:@"http://95.0.50.18:1249/kayit.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *tamanhoMensagem = [NSString stringWithFormat:@"%d", [mensagemSOAP length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/UrunToplamiGetir"
forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:tamanhoMensagem forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[mensagemSOAP dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conexao = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(conexao){
webData = [NSMutableData data];
}else{
NSLog(@"Connection Error.");
}
}
- (void)addInputViewToTextField:(UITextField *)textField{
if (!_datePicker) {
_datePicker = [[UIDatePicker alloc]init];
//[_datePicker setTag:textField.tag];
[_datePicker setTag:Date1.tag];
[_datePicker setTag:Date2.tag];
[_datePicker setDatePickerMode:UIDatePickerModeDate];
[_datePicker setDate:[NSDate date]];
}
Date1.inputView = _datePicker;
Date2.inputView=_datePicker;
_autocompleteTextField.inputView=nil;
if (!_pickerToolBar) {
_pickerToolBar =[[UIToolbar alloc]initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,44)];
_pickerToolBar.barStyle =UIBarStyleBlackOpaque;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelButtonPressed:)];
UIBarButtonItem *flexibleSpace =[[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self
action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:@selector(doneButtonPressed:)];
[_pickerToolBar setItems:@[cancelButton,flexibleSpace, doneButton]];
}
textField.inputAccessoryView = _pickerToolBar;
}
-(void)doneButtonPressed:(id)sender{
if (!_dateFormatter) {
_dateFormatter = [NSDateFormatter new];
[_dateFormatter setDateFormat:@"dd.MM.yyyy"];
}
Date1.text = [_dateFormatter stringFromDate:_datePicker.date];
[Date1 resignFirstResponder];
Date2.text = [_dateFormatter stringFromDate:_datePicker.date];
[Date2 resignFirstResponder];
}
- (void)cancelButtonPressed:(id)sender{
[Date1 resignFirstResponder];
[Date2 resignFirstResponder];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
Date1 = textField;
Date2 = textField;
[self addInputViewToTextField:textField];
[self addInputViewToTextField:textField];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
}
我的错误在哪里?
答案 0 :(得分:0)
您只有1个日期选择器。这没关系,但您需要区分两个文本字段。当按下完成按钮时,您只需要使用日期更新其中一个:
-(void)doneButtonPressed:(id)sender{
...
UITextField *activeTextField = [Date1 isFirstResponder]?Date1:Date2; // presumably that you only have these 2 textfields
activeTextField.text = [_dateFormatter stringFromDate:_datePicker.date];
[activeTextField resignFirstResponder];
}
修改强>
在- (void)textFieldDidBeginEditing:(UITextField *)textField
注意到第二个问题。您不应为Date
字段指定新值,它们是不同的控件。您使用它们的方式有点令人困惑,您有1 UIDatePicker
和2 UITextFields
,您应该在datepicker
之间共享textfields
,但无法使用他们在同一时间:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//[self addInputViewToTextField:textField]; -- not needed, it does the same thing as the call below, textfield value doesn't change between the 2 calls
[self addInputViewToTextField:textField];
}
你还有其他地方:
[_datePicker setTag:Date1.tag];
[_datePicker setTag:Date2.tag];
这可能不会导致问题,但日期选择器只有1个标记,因此会从中丢弃Date1.tag
值,其值为Date2.tag
。这类似于:
_datePicker.tag = 1;
_datePicker.tag = 2; // 1 is discarded and replaced with 2