【问题标题】:How to use date picker instead of textfield?如何使用日期选择器而不是文本字段?
【发布时间】:2013-05-09 14:01:29
【问题描述】:

我有 2 个文本字段和 1 个按钮。我使用文本字段输入日期,但我必须使用日期选择器。

此代码用于在文本字段中输入日期并将某些字段显示为标签。我如何为日期选择器更改此代码?

ViewController.h

  @interface ViewController : UIViewController <NSXMLParserDelegate>

 - (IBAction)Send:(UIButton *)sender;
 @property (unsafe_unretained, nonatomic) IBOutlet UITextField *Date1;
 @property (unsafe_unretained, nonatomic) IBOutlet UITextField *Date2;
 //will here define for date picker
 @end

ViewController.m

 @interface ViewController (){
 NSMutableData *webData;
NSXMLParser *xmlParser;
NSMutableString *returnSOAP;
BOOL tReturn;


  }
 @end
 @implementation ViewController
 @synthesize Date1,Date2;

- (IBAction)Send:(UIButton *)sender{

    NSString *msgSOAP= [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"
                   "<ShowDetails xmlns=\"http://tempuri.org/\">\n"
                   "<date1>%@</date1>\n"
                   "<date2>%@</date2>\n"

                   "</ShowDetails>\n"
                   "</soap:Body>\n"
                   "</soap:Envelope>\n",Date1.text,Date2.text];


  NSLog(@"SOAP Resulr = \n%@\n\n", msgSOAP);

NSURL *url = [NSURL URLWithString:@"http://webservice/service.asmx"];

     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];       
     NSString *dnMsg = [NSString stringWithFormat:@"%d", [msgSOAP length]];

 [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
 [theRequest addValue: @"http://tempuri.org/ShowDetails" 
 forHTTPHeaderField:@"SOAPAction"];
[theRequest dnMsg forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[msgSOAP dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest  
delegate:self];

if(con){
    webData = [NSMutableData data];
}else{
    NSLog(@"Connection Error.");
}
}

谢谢

【问题讨论】:

  • 你想要的日期格式是什么?您需要使用 textField 的 inputView 和 inputAccessoryView 来显示 datePicker。选择一个日期,使用 dateFormatter 进行转换并将其设置为 textField 的文本。

标签: ios xcode soap datepicker uitextfield


【解决方案1】:

如果我让您正确,您希望使用 datePicker 以便用户输入正确的日期,而您不必验证输入的日期。

你可以使用textField的inputView。使用 inputAccessoryView 显示另一个视图,您可以在其中放置一些按钮来关闭 inputView。

@interface ViewController ()
{
    UIDatePicker *_datePicker;
    UIToolbar *_pickerToolBar;

    UITextField *_activeTextField;

    NSDateFormatter *_dateFormatter;
}

@end

@implementation ViewController

- (void)addInputViewToTextField:(UITextField *)textField{

    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        [_datePicker setDatePickerMode:UIDatePickerModeDate];
        [_datePicker setDate:[NSDate date]];
    }

    textField.inputView = _datePicker;

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

    _activeTextField.text = [_dateFormatter stringFromDate:_datePicker.date];
    [_activeTextField resignFirstResponder];

}

- (void)cancelButtonPressed:(id)sender{
    [_activeTextField resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _activeTextField = textField;
    [self addInputViewToTextField:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    _activeTextField = nil;
}

Source Code

【讨论】:

  • 这对我来说是一个很好的例子,谢谢。谢谢。如何更改日期格式,如 21.11.2012
  • @MehmetAkyel 很高兴知道您发现它很有用。我不知道你的 dateFormat 所以我用了一个通用的。 [_dateFormatter setDateFormat:@"dd.MM.yyyy"],而不是 dateStyle 和 timeStyle。并且不要忘记接受;)
  • 我输入了从 web servis 调用的日期字段。在 web servis 调用字段中的日期格式为 21.12.2012
  • @MehmetAkyel 我已经更新了我的答案,现在不工作了吗?
  • 它工作得非常好。非常感谢。我有一个问题要问你。我有 3 个文本字段。站 ID、日期 1、日期 2 文本文件。我只想将此代码用于 2 个文本文件。此代码是否适用于表单上的所有文本字段?
【解决方案2】:

使用这个

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  
{  
   //Check the textfield for which you want to display date picker
    if(textField == datePickerTextField)
    {
       [self showDatePicker];
       return NO; //to disallow editing i.e it will not open keyboard
    }
}

【讨论】:

  • 感谢您的回答,但我认为您没有理解我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多