【问题标题】:UIDatePicker get .date property on "done" button pressUIDatePicker 在“完成”按钮按下时获取 .date 属性
【发布时间】:2017-01-09 21:40:01
【问题描述】:

我有一个自定义视图,其中有一个 UIDatePicker 和一个附加在顶部的完成按钮。

当我按下完成按钮时,我想获得 UIDatePicker 中的时间

当我按下按钮时收到错误消息(我认为这是因为发件人是 UIBarButtonItem)并且更改日期时间按预期工作。

其他解决方案显示单独的选择器,但是,我希望(如果可能)调用相同的选择器并像在选择器视图中滚动一样获取被选中的对象。

我想更改选择器方法以将其作为id 接收,但没有奏效。

这就是我目前正在做的事情,我觉得这可能很愚蠢:

UIView *v = [[UIView alloc] init];

// Create the date time picker
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView = [[UIDatePicker alloc] initWithFrame:pickerFrame];

pView.datePickerMode = UIDatePickerModeTime;
[pView addTarget:self action:@selector(dateTimePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

dateTimePicker = pView;
[v addSubview:dateTimePicker];

// done button
CGRect toolBarFrame = CGRectMake(0, 0, 320, 44);
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dateTimePickerValueChanged:)];
NSArray *toolBarItems = [NSArray arrayWithObjects:flexibleItem, doneButton, nil];
[toolBar setItems:toolBarItems];
[v addSubview:toolBar];

- (void)dateTimePickerValueChanged:(UIDatePicker *)datePicker {
    NSLog(@"time on change: %@", datePicker.date);

图片参考:

【问题讨论】:

    标签: ios objective-c uidatepicker


    【解决方案1】:

    编写不带参数的方法并将UIDatePicker 作为整个类的属性或全局对象,以便您可以从任何地方访问它。

    现在将您的方法传递给两个选择器,这样您将在按下完成按钮时获得日期。

    - (void)dateTimePickerValueChanged {
        NSLog(@"time on change: %@", datePicker.date);
    }
    

    【讨论】:

      【解决方案2】:

      你说得对导致这个崩溃的原因是正确的。我认为分离选择器是正确的道路。您可以让两个选择器调用相同的方法。这样你就没有任何复制的代码。

      【讨论】:

      • 如果我要分离选择器,我将如何获得选择器视图中的时间?我的问题是试图弄清楚如何将日期实例放入完成的选择器中。除非我不能那样做?
      • 使日期选择器成为类的属性。您可以通过这种方式访问​​它。
      【解决方案3】:

      执行此操作的正确方法是创建一个NSDate 对象,该对象可根据您的要求进行访问。现在从UIDatePicker,创建一个改变值的方法。每当调用此方法时,将日期选择器 .date 属性的值设置为您的 NSDate 对象。 在按下完成按钮时,使用 NSDate 对象的值。它会给你正确的价值。

      @interface ViewController(){
      NSDate *globalDate;
      }
      @end
      
      @implementation ViewController 
      
      - (void)dateTimePickerValueChanged {
          globalDate = datePicker.date;
      }
      
      - (IBAction)donePressed {
      NSLog(@"%@",globalDate);
      }
      
      @end
      

      【讨论】:

        【解决方案4】:

        时间

         datePicker=[[UIDatePicker alloc]init];
        datePicker.datePickerMode=UIDatePickerModeTime;
        [yourField setInputView:datePicker];
        UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        [toolBar setTintColor:[UIColor grayColor]];
        UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];
        
        
        UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        
        
        [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
        [yourField setInputAccessoryView:toolBar];
        
        
        
        -(void)ShowSelectedDate
        {
        NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
        dateFormat.dateStyle=NSDateFormatterMediumStyle;
        [dateFormat setDateFormat:@"hh:mm"];
        NSString *str=[NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:datePicker.date]];
        //assign text to label
        yourField =str;
        [yourField resignFirstResponder];
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
        }
        

        【讨论】:

        • 你应该使用 UIView 动画块而不是旧的传统动画样式。
        猜你喜欢
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多