【问题标题】:Why first date picker take selected date value from second date picker?为什么第一个日期选择器从第二个日期选择器中获取选定的日期值?
【发布时间】:2013-05-20 17:42:30
【问题描述】:

我有 2 个日期选择器和 1 个按钮。当我从选择器中选择日期时,第一个选择器获取第二个选择器值。我从第一个选择器中选择 19.05.2013,从第二个选择器中选择 20.05.2013,但输出类似;

输出日志

20.05.2013第一个选择器

20.05.2013 第二个选择器

.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{

}

我的错误在哪里?

【问题讨论】:

    标签: ios xcode datepicker uitextfield


    【解决方案1】:

    您只有 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,你应该在textfields之间共享datepicker,但不能同时使用它们:

    - (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
    

    【讨论】:

    • 抱歉不工作。有同样的问题。当我选择日期第一个和第二个选择器时,第一个和第二个是相等的。
    • 抱歉,这就是我在您发布的代码中可以找到的所有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多