如何使用一个UIActionSheet获得两个不同的日期选择器?

时间:2012-02-14 08:12:44

标签: iphone uitextfield uiactionsheet

我有多个UITextField,我为每个textField指定了标签,我调用了datepicker而textfield didBinging Editing.i获得了不同类型的选择器和删除prepossess的datepicker我正在使用UIActinSheet ..我的问题是,我得到了datePicker只有一次我选择下一个TextField它会崩溃...

请帮助我 我给了代码我做了什么。?

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    if (textField.tag==11) {
        NSLog(@"text fiels work out>>%d",textField.tag);

        //UIActionSheet
        pickerActionSheet  =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Done" destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerActionSheet.actionSheetStyle=UIActionSheetStyleDefault;
        //date picker    
        insertDatePicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 70, 325, 300)];
        insertDatePicker.datePickerMode=UIDatePickerModeDate;
        insertDatePicker.date=[NSDate date];
        [insertDatePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        //[self.view addSubview:insertDatePicker];
        [pickerActionSheet addSubview:insertDatePicker];
        [insertDatePicker release];
        [pickerActionSheet showFromTabBar:self.tabBarController.tabBar];
        [pickerActionSheet setBounds:CGRectMake(0, 0, 320, 464)];
        [pickerActionSheet release];
        insertDatePicker.hidden=NO;
        timeDatePicer.hidden=YES;


    }else 
    {
        NSLog(@"text fiels work out>>%d",textField.tag);

      //UIActionSheet
        pickerActionSheet  =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Done" destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerActionSheet.actionSheetStyle=UIActionSheetStyleDefault;

        //time picker
        timeDatePicer=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 230, 325, 300)];
        timeDatePicer.datePickerMode=UIDatePickerModeTime;
        timeDatePicer.date=[NSDate date];
        [timeDatePicer addTarget:self action:@selector(timeChanged:)     forControlEvents:UIControlEventValueChanged];
        // [self.view addSubview:timeDatePicer];
         [pickerActionSheet addSubview:timeDatePicer];
        [timeDatePicer release];
        [pickerActionSheet showFromTabBar:self.tabBarController.tabBar];
        [pickerActionSheet setBounds:CGRectMake(0, 0, 320, 464)];
        [pickerActionSheet release];

        timeDatePicer.hidden=NO;
        insertDatePicker.hidden=YES;


    } 

    textFieldDidBeginEditing=textField;
    [textField resignFirstResponder];

}

2 个答案:

答案 0 :(得分:1)

在.h文件中

合成

   UIActionSheet *_pickerActionSheet;
   @property (nonatomic,retain)UIActionSheet *pickerActionSheet;

在.m文件中

    @synthesize pickerActionSheet =_pickerActionSheet;

    where "pickerActionSheet" replace By "self.pickerActionSheet"
    don't release "pickerActionSheet" here 

在Dealloc方法中

 release Here 
   [_pickerActionSheet release];

答案 1 :(得分:1)

无需隐藏insertDatePicker和timeDatePicer。一旦释放pickerView,它将被删除。

这也是错误删除此行。

textFieldDidBeginEditing =文本字段;

我已经清理了你的代码。现在工作正常。

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag==11) {
        NSLog(@"text fiels work out>>%d",textField.tag);
        //UIActionSheet
        pickerActionSheet  =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Done" destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerActionSheet.actionSheetStyle=UIActionSheetStyleDefault;
        //date picker    
        insertDatePicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 70, 325, 300)];
        insertDatePicker.datePickerMode=UIDatePickerModeDate;
        insertDatePicker.date=[NSDate date];
        [insertDatePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        //[self.view addSubview:insertDatePicker];
        [pickerActionSheet addSubview:insertDatePicker];
        [insertDatePicker release];
    [pickerActionSheet showFromTabBar:self.tabBarController.tabBar];
        [pickerActionSheet setBounds:CGRectMake(0, 0, 320, 464)];

    }else 
    {
        NSLog(@"text fiels work out>>%d",textField.tag);
        //UIActionSheet
        pickerActionSheet  =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Done" destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerActionSheet.actionSheetStyle=UIActionSheetStyleDefault;

        //time picker
        timeDatePicer=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 70, 325, 300)];
        timeDatePicer.datePickerMode=UIDatePickerModeTime;
        timeDatePicer.date=[NSDate date];
        [timeDatePicer addTarget:self action:@selector(timeChanged:) forControlEvents:UIControlEventValueChanged];
        [pickerActionSheet addSubview:timeDatePicer];
        [timeDatePicer release];
    [pickerActionSheet showFromTabBar:self.tabBarController.tabBar];

        [pickerActionSheet setBounds:CGRectMake(0, 0, 320, 464)];
        [pickerActionSheet release];

    } 
    [textField resignFirstResponder];

}