【问题标题】:Replacing UITextField input with a UIDatePicker用 UIDatePicker 替换 UITextField 输入
【发布时间】:2012-06-19 10:12:12
【问题描述】:

我有一个表格视图控制器(其中包括一个代表日期的单元格)。我关注了这篇文章“How do I make a modal date picker that only covers half the screen?”,但我遇到了一个很大的例外——我无法让选择器消失!

我尝试注册事件 UIControlEventTouchUpOutside,但似乎这不是由选择器生成的(或者至少在我使用它的模式下)。

如何识别用户已完成日期选择?

另外,有没有办法禁止用户直接输入到 UITextField?我想强迫他们使用选择器。我看到了这个帖子“Disable blinking cursor in UITextField?”,但是还有其他方法吗?

问候,

--约翰

【问题讨论】:

    标签: ios uitextfield modal-dialog uidatepicker


    【解决方案1】:

    试试这个代码..这里我把一个日期选择器放到一个uitextfield..它会在右上角导航栏有一个完成按钮..所以通过点击完成我将用户可以关闭日期选择器..另一个最好的方法是通过在具有完成按钮的日期选择器上方放置一个工具栏..试试这个它会起作用..更改日期选择器时,您可以填充文本字段..希望这会有所帮助..

    查看这个stackoverflow链接https://stackoverflow.com/a/4824319/763747这将有日期选择器和完成按钮作为键盘上方的工具栏..

    #pragma mark -
    #pragma mark - TextField Delegate
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
        [textField resignFirstResponder];
        return TRUE;
    }
    
    
    - (void) textFieldDidBeginEditing:(UITextField *)textField {
        itsRightBarButton.title  = @"Done";
        itsRightBarButton.style = UIBarButtonItemStyleDone;
        itsRightBarButton.target = self;
        itsRightBarButton.action = @selector(doneAction:);
        if ([textField isEqual:itsIncidentDateTextField])
        {
            itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
            itsDatePicker.datePickerMode = UIDatePickerModeDate;
            [itsDatePicker addTarget:self action:@selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
            //datePicker.tag = indexPath.row;
            textField.inputView = itsDatePicker;
        }
    }
    
    - (IBAction) incidentDateValueChanged:(id)sender{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM d, yyyy"];
        itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
        [dateFormatter release];
    }
    

    【讨论】:

    • 我知道这是联系人中使用的技术,假设您添加生日。我将不得不修改我的视图,因为我目前有一个“取消”按钮作为右栏按钮项。我会试一试。感谢您的回答!
    • 正是我所做的!再次感谢。
    【解决方案2】:

    我使用了另一种方法来使 UIDatePicker 消失。

    创建一个 XIB (nib) 文件并向其中添加一个 UIDatePicker,然后调整视图大小以使其仅适合 UIDatePicker,在您的 ViewController(或您使用的任何类,并合成课程)。

    @property (nonatomic, strong) UIView *myDatePickerView;  
    @synthesize myDatePickerView;
    

    然后创建一个 loadDatePickerView 方法

    - (void) loadDatePickerView
    {
        UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]];
        NSArray *views = [nib instantiateWithOwner:self options:nil];
        self.myDatePickerView = [views firstObject];
        [myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)];
        [self.view addSubview:myDatePickerView];
    }
    

    实现UITextFieldDelegate,并在textFieldDidBeginEditing方法中调用loadDatePickerView方法

    [self loadDatePickerView];
    

    让函数创建一个属性,它是一个 UIDatePicker 实例

    @property (nonatomic,strong) 
    
    UIDatePicker *myDatePicker;
    

    (当然是综合)

    现在像这样创建一个 IBAction

    -(IBAction)datePickerValueChanged:(UIDatePicker *)sender
    {
        myDatePicker = [[myDatePickerView subviews] lastObject];
        //now you can do whatever you want with the DatePicker
    }
    

    现在将 IBAction 连接到 XIB 文件中的选取器,这样 XIB 现在就是您在 VC 中创建的 UIDatePicker 实例,如果您希望它消失,您可以添加 UITapGestureRecognizer(在 ViewDidLoad 中) 并且选择器将是另一个 IBAction,它会从它的 superView 中删除 myDatePickerView,如下所示:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self     action:@selector(dropPicker:)];
        [self.view addGestureRecognizer:tap];
        [self datePickerValueChanged:myDatePicker];
    }
    
    -(IBAction)dropPicker:(id)sender
    {
        [myDatePickerView removeFromSuperview];
    } 
    

    【讨论】:

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