【问题标题】:ios UITextField to edit with uidatepickerios UITextField 使用 uidatepicker 进行编辑
【发布时间】:2011-06-11 09:53:15
【问题描述】:

我有一个写有日期的 uitextfield。如何在页面底部显示 uidatepicker 而不是标准键盘? 提前致谢

【问题讨论】:

    标签: ios uiviewcontroller uitextfield editing uidatepicker


    【解决方案1】:

    确保你的类实现了UITextFieldDelegate 协议。然后,重写shouldBeginEditing委托方法返回FALSE:

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

    我建议使用另一个包含UIDatePickerUIViewController 并使用presentModalViewController 来显示它。这符合用户界面指南。

    【讨论】:

    • 如果您只希望日期选择器位于屏幕底部,而不是覆盖整个屏幕,则此方法存在问题。您不能使模态呈现的视图控制器透明,因为底层视图不会存在。 stackoverflow.com/questions/849458/…
    【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        相关资源
        最近更新 更多