如何使用日期选择器而不是文本字段?

时间:2013-05-09 14:01:30

标签: ios xcode soap datepicker uitextfield

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

此代码用于输入日期到文本字段并显示一些字段到标签。如何更改日期选择器此代码?

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.");
}
}

谢谢

2 个答案:

答案 0 :(得分:0)

使用此

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

答案 1 :(得分:0)

如果我告诉你你想要使用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

相关问题