0

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

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

视图控制器.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

视图控制器.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.");
}
}

谢谢

4

2 回答 2

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

源代码

于 2013-05-09T16:23:09.497 回答
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
    }
}
于 2013-05-09T14:03:53.010 回答