2011-01-18 13 views
6

के साथ संपादित करने के लिए मेरे पास एक यूटेक्स्टफील्ड है जहां एक तारीख लिखा गया है .. मैं पृष्ठ के निचले भाग को एक मानक कीबोर्ड के बजाय यूआईडीएटीपीकर कैसे दिखा सकता हूं? अग्रिम धन्यवादios UITextField uidatepicker

उत्तर

4

सुनिश्चित करें कि आपकी कक्षा UITextFieldDelegate प्रोटोकॉल लागू करती है। फिर, FALSE वापस जाने के लिए shouldBeginEditing प्रतिनिधि विधि ओवरराइड:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    //check if it is the UITextField you don't want the keyboard for 
    if (textField != dateTextField) 
     return TRUE; 

    //present your UIDatePicker 
    //see instructions below 

    //Return false to prevent the keyboard from appearing. 
    return FALSE; 
} 

मैं एक UIViewController कि एक UIDatePicker शामिल का उपयोग करना चाहिये और presentModalViewController का उपयोग यह दिखाने के लिए। यह उपयोगकर्ता-इंटरफेस दिशानिर्देशों के अनुरूप है।

+2

इस दृष्टिकोण के साथ कोई समस्या है, यदि आप केवल स्क्रीन के नीचे डेटपिकर चाहते हैं, और पूरी स्क्रीन को कवर नहीं करते हैं। आप अपना सामान्य रूप से प्रस्तुत दृश्य नियंत्रक पारदर्शी नहीं बना सकते हैं, क्योंकि अंतर्निहित विचार वहां नहीं होंगे। http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller – kris

5

UITextField इसके लिए एक तंत्र प्रदान करता है। आप inputView के रूप में एक और दृश्य (एक डेटपिकर की तरह) सेट करने में सक्षम होना चाहिए। यह पोस्ट देखें:

iPhone datepicker instead of keyboard?

3

आप नीचे दिए गए कोड का उपयोग कर सकते हैं;

//first of all, you have to declare the datePicker and then use the following code; 

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
[dateLabel resignFirstResponder]; //the textField that you will set the selected date 
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component 

pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"Select the date!" 
              delegate:self 
            cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)]; 
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; 
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; //or another LocaleIdentifier instead of en_US 
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; //desired format 

[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker 

//now preparing the toolbar which will be displayed at the top of the datePicker 
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
pickerToolbar.barStyle=UIBarStyleBlackOpaque; 
[pickerToolbar sizeToFit]; 
NSMutableArray *barItems = [[NSMutableArray alloc] init]; 
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button. 
[barItems addObject:flexSpace]; // set the left of the bar 

[pickerToolbar setItems:barItems animated:YES]; 
[pickerViewDate addSubview:pickerToolbar]; 
[pickerViewDate addSubview:datePicker]; 
[pickerViewDate showInView:self.view]; 
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position 
} 

और अतिरिक्त क्रियाएं;

-(IBAction)dateChanged{ 
    NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init]; 
    [FormatDate setLocale: [[NSLocale alloc] 
         initWithLocaleIdentifier:@"en_US"]]; 
    [FormatDate setDateFormat:@"dd.MM.yyyy"]; 
    dateLabel.text = [FormatDate stringFromDate:[datePicker date]]; 
} 

-(BOOL)closeDatePicker:(id)sender{ 
    [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES]; 
    [dateLabel resignFirstResponder]; 
    return YES; 
} 

-(IBAction)doneButtonClicked{ 
    [self closeDatePicker:self]; 
}