【问题标题】:ios 6: UIDatePicker in the UITextFieldios 6:UITextField 中的 UIDatePicker
【发布时间】:2012-12-11 03:09:46
【问题描述】:

我在将日期选择器与文本字段连接时遇到问题。我已经尝试了 stackoverflow 中的所有解决方案,但它们在 ios 6 中不起作用。你能描述一下如何制作吗,我想点击文本字段并通过 datepicker 选择日期。

【问题讨论】:

    标签: ios ios6 datepicker uitextfield


    【解决方案1】:

    您应该使用UITextField 的inputView 属性来使选择器代替键盘显示。然后,iOS6 将为您处理所有事情(使用动画而不是键盘等显示选取器)。

    注意:您可能还想添加一些inputAccessoryView(通常是带有一些“确定”按钮的UIToolBar)以使用户能够关闭选择器(IBAction您的“确定”按钮将简单地调用[textField resignFirstResponder] 以实现这一点)因为UIDatePicker 没有任何按钮来验证您的输入(而键盘有其“返回键”)

    【讨论】:

      【解决方案2】:
      #import "TextfieldwithDatepickerViewController.h"
      
      UIActionSheet *pickerViewPopup;
      
      @implementation TextfieldwithDatepickerViewController
      - (void)textFieldDidBeginEditing:(UITextField *)aTextField{  
          [aTextField resignFirstResponder];  
      
          pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];  
      
          UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
          pickerView.datePickerMode = UIDatePickerModeDate;  
          pickerView.hidden = NO;  
          pickerView.date = [NSDate date];  
      
          UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
          pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
          [pickerToolbar sizeToFit];  
      
          NSMutableArray *barItems = [[NSMutableArray alloc] init];  
      
          UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
          [barItems addObject:flexSpace];  
      
          UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
          [barItems addObject:doneBtn];  
      
          UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
          [barItems addObject:cancelBtn];  
      
          [pickerToolbar setItems:barItems animated:YES];  
      
          [pickerViewPopup addSubview:pickerToolbar];  
          [pickerViewPopup addSubview:pickerView];  
          [pickerViewPopup showInView:self.view];  
          [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];  
      }  
      
      -(void)doneButtonPressed:(id)sender{  
          //Do something here here with the value selected using [pickerView date] to get that value  
      
          [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
      }  
      
      -(void)cancelButtonPressed:(id)sender{  
          [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
      }  
      

      【讨论】:

      • 我成功了,它编译并启动了,但是pickview或picktoolbar没有出现。刚刚出现了标准键盘。你能在文本字段中给我日期选择器的项目来源吗?也许你可以把它放到 github 上?
      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多