2013-05-09 76 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

相關問題