2013-05-09 53 views
0

我有2个文本框和1个按钮。我使用textfield输入日期,但我必须使用日期选择器。如何使用日期选择器而不是文本字段?

此代码用于输入日期到文本字段并显示一些字段到labels.How我更改日期选择器此代码?

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

谢谢

+0

什么是你想要的日期的格式?您需要使用textField的inputView和inputAccessoryView来显示一个datePicker。选择一个日期,使用dateFormatter将其转换并将其设置为textField的文本。 – Anupdas 2013-05-09 16:16:25

回答

0

如果我得到你纠正你想使用日期选择器所以该用户输入正确的日期,并且不必验证输入的日期。

您可以使用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

+0

这是我很好的例子谢谢你。谢谢。我如何更改日期格式,如21.11.2012 – Mhmt 2013-05-10 07:13:15

+0

@MehmetAkyel很高兴知道你觉得它有用。我不知道你的dateFormat,所以我使用了一个通用的。 '[_dateFormatter setDateFormat:@“dd.MM.yyyy”]',而不是dateStyle和timeStyle。并且不要忘记接受;) – Anupdas 2013-05-10 07:15:49

+0

我输入的日期字段为从网络servis.In叫网络servis调用字段有21.12.2012日期格式 – Mhmt 2013-05-10 07:19:30

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 
    } 
} 
+0

谢谢你的回答,但我认为你不明白我的问题 – Mhmt 2013-05-09 14:55:28

相关问题