带有args的UIDatePicker的选择器(UITextfiled)

时间:2012-01-30 12:00:50

标签: objective-c datepicker selector

我看到很多文本字段都触发了一个日期选择器。 我想只有一种方法来制作一个日期选择器。此日期选择器必须具有方法选择器,其中包含文本字段的标识符。我不知道怎么能做到这一点。

这是我的代码:

- (IBAction)launchPickerDate:(id)sender{
    UIDatePicker *datepickerNew = [UIDatePicker new];
    [datepickerNew setHidden:NO];
    datepickerNew.frame =  CGRectOffset(datepickerNew.frame, 0, 244);

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    UIDatePicker *launchPickerDate = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [launchPickerDate addTarget:self action:@selector(SelectDatePicker:) forControlEvents:UIControlEventValueChanged];

    [actionSheet addSubview:launchPickerDate];

    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];
    [closeButton addTarget:self action:@selector(closeDatePicker:) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

1 个答案:

答案 0 :(得分:0)

我没有真正得到您的问题,但在我看来,您希望根据所选的文本字段对日期选择器进行一些更改。您可以使用tag属性识别调用该方法的文本字段。像

if (sender.tag==1){
//do something as first text field selected
}else if(sender.tag==2){

//do something as second text field selected
}

您可以通过界面构建​​器(如果您使用的是xib)或通过代码等指定文本字段的标记。 firstTextField.tag=1; 等等..希望这有帮助......如果还有其他事情让我知道