UIDatePicker、UIActionSheet 和数据处理

发布于 2024-12-20 18:54:26 字数 1335 浏览 1 评论 0原文

所以我知道如何从日期选择器中提取日期。但我似乎无法从 UIActionSheet 上的日期选择器中提取它。我们当前的设置是一个使用几个选择器输入数据的表单,所有选择器都使用数据源的 didSelectRow 方法,并且所有选择器都使用相同的视图来表示数据源。

现在看来,这对于 UIDatePicker 来说并不适用。这是迄今为止我的日期选择器的内容,它与我的其他选择器类似,包括处理值更改的方法。

-(IBAction)ageBox{
    pickerAction=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [pickerAction setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    UIDatePicker *datePicker =[[UIDatePicker alloc]initWithFrame:pickerFrame];

    [pickerAction addSubview:datePicker];

    UISegmentedControl *closeButton=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObject:@"OK"] ];
    closeButton.momentary = YES;
    closeButton.frame=CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle =UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    date = datePicker.date;
    [closeButton addTarget:self action:@selector(closePicker) forControlEvents:UIControlEventValueChanged];
    [pickerAction addSubview:closeButton];
    whatToChange = 5;
    [pickerAction showInView:self.view];
    [pickerAction setBounds:CGRectMake(0, 0, 320, 464)];
}

当日期位于操作表上时,或者当我单击“确定”时,在日期选择器被销毁之前从日期选择器中提取日期,有什么帮助吗?

So I understand how to pull the date from a date picker just fine. But I can't seem to be able to pull it from a date picker on a UIActionSheet. Our current set up is a form for entering data with a few pickers, all using the didSelectRow method for the data source, all of them using the same view for said data source.

Now this doesn't work for a UIDatePicker in the same way, it seems. Here is what I have so far for my date picker, it's similar to my other pickers, including the methods to handle changing of values.

-(IBAction)ageBox{
    pickerAction=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [pickerAction setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    UIDatePicker *datePicker =[[UIDatePicker alloc]initWithFrame:pickerFrame];

    [pickerAction addSubview:datePicker];

    UISegmentedControl *closeButton=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObject:@"OK"] ];
    closeButton.momentary = YES;
    closeButton.frame=CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle =UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    date = datePicker.date;
    [closeButton addTarget:self action:@selector(closePicker) forControlEvents:UIControlEventValueChanged];
    [pickerAction addSubview:closeButton];
    whatToChange = 5;
    [pickerAction showInView:self.view];
    [pickerAction setBounds:CGRectMake(0, 0, 320, 464)];
}

Any help pulling the date from the date picker while it's on the action sheet, or when I click ok, pulling it from it before it's destroyed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-12-27 18:54:26

根据方法中的第一行代码,我假设 pickerActionselfivar。我可以想到几种方法,我相信还有更多。

1) 将 datePicker 也设为 ivar。这样您就可以为您的 'ok' 方法引用它。

2) 从pickerAction本身获取引用。由于您已将 datePicker 添加为 subview,因此您可以像这样找到它:

    // Somewhere in your "ok" method
    for (UIView *view in pickerAction.subviews) {
        if ([view isKindOfClass:[UIDatePicker class]]){
            UIDatePicker *picker = (UIDatePicker *)view;
            NSDate *pickedDate = picker.date;
        }
    }

I'm making the assumption that pickerAction is an ivar of self, based on the first line of code in the method. I can think of a couple of ways, I'm sure there are more.

1) Make datePicker an ivar as well. That way you have a reference to it for your 'ok' method.

2) Obtain the reference from the pickerAction itself. Since you have added the datePicker as a subview, you can find it like so:

    // Somewhere in your "ok" method
    for (UIView *view in pickerAction.subviews) {
        if ([view isKindOfClass:[UIDatePicker class]]){
            UIDatePicker *picker = (UIDatePicker *)view;
            NSDate *pickedDate = picker.date;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文