【问题标题】:datepicker by clicking on textfield [duplicate]通过单击文本字段来选择日期[重复]
【发布时间】:2011-09-05 13:25:22
【问题描述】:

我正在尝试通过单击文本字段来实现日期选择器。我正在使用this link 中提到的代码。

但我没有得到输出。如果有人知道如何通过单击文本字段来带上日期选择器。请给我一些解决方案。

提前致谢。

【问题讨论】:

  • 在您的文本字段上放置透明按钮并在点击事件时显示日期选择器。或者你可以使用文本字段委托方法。
  • “输出”是什么意思?如何将选择的日期作为字符串放入文本字​​段?

标签: iphone


【解决方案1】:

我看到了您的链接并得到了解决方案,因此请尝试以下代码。

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

此代码绝对可以帮助您,因为我运行该代码并且它工作正常。

【讨论】:

  • 真的很感谢你帮助我亲爱的。它工作正常。非常非常感谢。
  • 工作正常,但是当我点击完成并取消时,它没有响应。只有轮子在旋转。
【解决方案2】:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)txtField
{
[self createDatePicker];
}

同时设置 Delegate TextFeild

【讨论】:

    【解决方案3】:

    您可以将日期选择器放在界面构建器的另一个视图中...并将视图放在主视图下(x = 0; y = 460)。

    因此,当您的文本字段成为第一响应者时(有一些委托方法,例如 - textFieldDidBeginEditing:) ...只需在所需位置使用 datePicker 为视图设置动画。

    动画可以像 ...

    [UIView beginAnimations:@"some_random_name" context:nil];
    
        //give your date picker view a new frame
    
    [UIView setAnimationDuration:0.3f]; //or any other value
    [UIView commitAnimations];
    

    【讨论】:

      【解决方案4】:

      检查您是否为文本字段设置了委托。 如果没有,设置如下:

      textField.delegate = self;
      

      或者在 xib 中添加到委托的连接。

      【讨论】:

        【解决方案5】:

        我们可以使用UITextField的inputViewinputAccessoryView。 在 .h 文件中声明,在 .m 文件中声明 @synthesize。

        @property (nonatomic, retain) IBOutlet UIToolbar    *_accessoryView;
        @property (nonatomic, retain) IBOutlet UIDatePicker *_datePickerView;
        

        将它们连接到 .xib 文件中的 File's Owner,确保它不在 self.view 中。

        - (void)viewDidLoad
            {   
                _dateOfBirthTextField.inputView = self._datePickerView;
                _dateOfBirthTextField.inputAccessoryView = self._toolbar;
            }
        

        还要编写函数从选择器中获取日期

        #pragma mark- UITextField with UIDatePicker-
        
        - (IBAction)dateChanged:(id)sender {
            UIDatePicker *picker = (UIDatePicker *)sender;
            _dateOfBirthTextField.text = [NSString stringWithFormat:@"%@", picker.date];
        }
        
        - (IBAction)doneEditing:(id)sender {
            [_dateOfBirthTextField resignFirstResponder];
        }
        

        在 valueChanged 上添加 dateChanged:IBAction:_datePicker 的操作。 和 doneEditing: IBAction on toolbar barbutton action。

        【讨论】:

          【解决方案6】:

          使用以下代码:

          UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
          [actionSheet showInView:self.tabBarController.tabBar];
          [actionSheet setFrame:CGRectMake(0, 220, 320,350)];
          pickerToolbar.barStyle = UIBarStyleDefault;
          [pickerToolbar sizeToFit];
          pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
          
          NSMutableArray *barItems = [[NSMutableArray alloc] init];
          
          UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Cancel"] style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
          [barItems addObject:cancelBtn];
          [cancelBtn release];
          UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
          [barItems addObject:flexSpace];
          [flexSpace release];
          UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
          [barItems addObject:doneBtn];
          [doneBtn release];
          [pickerToolbar setItems:barItems animated:YES];
          [actionSheet addSubview:pickerToolbar];
          [barItems release];
          [pickerToolbar release];
          UIDatePicker *datePicker=[[UIDatePicker alloc]init];
          if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
          {
              datePicker.frame=CGRectMake(0,32,480, 216); 
              [scrollView setContentOffset:CGPointMake(0, 60) animated:YES];
          } else {
              datePicker.frame=CGRectMake(0,44,320, 216);
          }
          
          datePicker.datePickerMode = UIDatePickerModeDateAndTime;
          [datePicker setMinuteInterval:5];
          [datePicker setTag:10];
          [actionSheet addSubview:datePicker];
          [datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
          [datePicker release];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-07-11
            • 1970-01-01
            • 2017-10-14
            • 1970-01-01
            • 2012-10-01
            • 2011-08-29
            • 1970-01-01
            相关资源
            最近更新 更多