如果用户以编程方式点击UITextfield,如何显示UIDatePicker

时间:2014-01-09 05:05:10

标签: ios iphone objective-c xcode

我想仅在用户单击UITextField时才显示UIDatePicker。选择日期时,它应该显示在同一个UITextField中。我想以编程方式实现UIDatePicker。我知道如何以编程方式编写UITextField的代码。 我甚至不知道如何调用UIDatePicker。

Birth.m

的日期
UITextField txtDOB=[[UITextField alloc]initWithFrame:CGRectMake(10, 190, 300, 20)];

txtDOB.borderStyle=UITextBorderStyleNone;

txtDOB.backgroundColor=[UIColor clearColor];

[txtDOB setUserInteractionEnabled:NO];

txtDOB.font=[UIFont systemFontOfSize:14.0];

txtDOB.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;

txtDOB.textAlignment=NSTextAlignmentLeft;

txtDOB.delegate=self;

[self.view addSubview:txtDOB];

7 个答案:

答案 0 :(得分:36)

最简单的方法是实现datePicker如下:

创建datePicker:

self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

然后将datePicker链接到textfield inputView:

self.textField.inputView = self.pickerView;

最后,您可以在选择日期时捕获操作,并将日期转换为字符串,以在textField

中显示所选日期
- (void)onDatePickerValueChanged:(UIDatePicker *)datePicker
{
    self.textField.text = [self.dateFormatter stringFromDate:datePicker.date];
}

答案 1 :(得分:7)

    UIDatePicker *picker1   = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
    [picker1 setDatePickerMode:UIDatePickerModeDate];
    picker1.backgroundColor = [UIColor whiteColor];
    [picker1 addTarget:self action:@selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];

//    [picker1 addSubview:toolBar];
    self.birthTxt.inputView  = picker1; // Here birthTxt is Textfield Name replace with your name

答案 2 :(得分:2)

在转到此代码之前,您需要了解UIActionSheet并且需要实施UIActionShetDelegateUITextFieldDelegatetextFieldDidBeginEditing中您需要显示操作表如下:

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
   UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Select Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
  [asheet showInView:[self.view superview]];
  [asheet setFrame:CGRectMake(0, 117, 320, 383)];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
  {
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 240)];

   [pickerView setMinuteInterval:5];
   [pickerView setTag: DatePickerTag];

  [actionSheet addSubview:pickerView];
  NSArray *subviews = [actionSheet subviews];

  [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 250, 280, 46)];
  [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 300, 280, 46)];

}

答案 3 :(得分:1)

在UITexfield上开始编辑时需要添加适当的选择器,使用下面的代码

 [txtDOB addTarget:self action:@selector(textbeginEditing:) forControlEvents:UIControlEventEditingDidBegin];

然后实现textbeginEditing方法以打开datepicker。

-(void)textbeginEditing:(id)sender{

UIActionSheet __autoreleasing * date_sheet = [[UIActionSheet alloc] initWithTitle:@"Select the date" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

date_sheet.tag = 2;

[date_sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];

datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];

// Implement the method labelChange
 [datePicker addTarget:self
                action:@selector(labelChange:)
      forControlEvents:UIControlEventValueChanged];


[date_sheet addSubview:datePicker];

UISegmentedControl __autoreleasing *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];

closeButton.momentary = YES;    
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);    
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;    
closeButton.tintColor = [UIColor blackColor];

[closeButton addTarget:self action:@selector(dissmissDatePicker:) forControlEvents:UIControlEventValueChanged];

[date_sheet addSubview:closeButton];
[date_sheet showInView:[[UIApplication sharedApplication] keyWindow]];    
[date_sheet setBounds:CGRectMake(0, 0, 320, 400)];
}

labelChange方法实现

-(void)labelChange:(UIDatePicker *)sender{
NSDateFormatter __autoreleasing *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
 dateFormat.dateStyle = NSDateFormatterMediumStyle;
 NSString *dateString =  [dateFormat stringFromDate:sender.date];
 txtDOB.text = dateString; 
}

答案 4 :(得分:1)

  • 首先表明您的班级符合UITextFieldDelegate协议

  • 然后在viewDidLoad中设置文字字段的delegatetag

     yourTextField.delegate = self;  
     yourTextField.tag = 111;
    
  • 然后实现以下委托方法:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {   
        if (textField.tag==111) {  
            [self showDatePicker];  
        }  
        return YES;  
    }
    
  • 现在实现一个函数showDatePicker

    - (void)showDatePicker {  
        datePicker = [[UIDatePicker alloc] init];
        datePicker.backgroundColor=[UIColor whiteColor];
    
        UIToolbar *toolbar = [[UIToolbar alloc]init];
        [toolbar sizeToFit];
    
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(showSelectedDate)];    
    
        doneBtn.tintColor = [UIColor whiteColor];  
    
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];    
    
        toolbar.backgroundColor = [UIColor colorWithRed:5.0/255.0 green:110.0/255.0 blue:170.0/255.0 alpha:1];  
    
        [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft,doneBtn ,nil]];  
    
        [yourTextField setInputAccessoryView:toolBar];  
    
        yourTextField.inputView = datePicker;   
    }
    

或者您只需在viewDidLoad中执行此操作,如下所示:

datePicker = [[UIDatePicker alloc] init];
datePicker.backgroundColor = [UIColor whiteColor];  

UIToolbar *toolbar = [[UIToolbar alloc]init]; 

[toolbar sizeToFit];    

 UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self
 action:@selector(showSelectedDate)];    

 doneBtn.tintColor = [UIColor whiteColor];  

 UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
 target:nil action:nil];    

toolbar.backgroundColor = [UIColor colorWithRed:5.0/255.0 green:110.0/255.0 blue:170.0/255.0 alpha:1];  

[toolBar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneBtn , nil]];  

[yourTextField setInputAccessoryView:toolbar];  

yourTextField.inputView = datePicker;

答案 5 :(得分:0)

这很简单,试试这个 当U单击textField时,将调用此方法。

-(void)textFieldDidBeginEditing:(UITextField *)textField
{  

然后,我们不想显示键盘。

[textField resignFirstResponder];  

然后,显示datePicker

datePicker=[[UIDatePicker alloc]init];
[datePicker setFrame:CGRectMake(<ur dimension>)];
datePicker.datePickerMode=UIDatePickerModeDateAndTime;
datePicker.timeZone = [NSTimeZone localTimeZone];
[datePicker addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged];  

这将显示datePicker 然后你想在textField

中显示所选的值
-(void)done
{
  NSString *dateStri=[dateFormater stringFromDate:datePicker.date];
    [timingTextField setText:[NSString stringWithFormat:@"%@",dateStri]];
}  

试试这个,并将结果更新

答案 6 :(得分:-1)

首先启用userIntraction

[txtDOB setUserInteractionEnabled:YES];

并将您的选择器视图添加为此文本提交的输入视图

[txtDOB setInputView:pickerview]