0

我有 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{

}

我的错误在哪里?

4

1 回答 1

0

You only have 1 date picker. This is ok, but you need to differentiate between the 2 textfields. You need to update only one of them with the date, when done button is pressed:

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

Edit:
Noticed a second issue at - (void)textFieldDidBeginEditing:(UITextField *)textField. You shouldn't assign a new value to your Date fields, they're different controls. The way you're using them is kind of confusing, you have 1 UIDatePicker and 2 UITextFields, you should share the datepicker between your textfields, but can't use them in the same time:

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

Somewhere else you have:

[_datePicker setTag:Date1.tag];
[_datePicker setTag:Date2.tag];

This probably doesn't cause an issue, but the date picker only has 1 tag, so Date1.tag value is discarded from it, it's value will be Date2.tag. This is similar to:

_datePicker.tag = 1;
_datePicker.tag = 2; // 1 is discarded and replaced with 2
于 2013-05-20T17:52:37.990 回答