简体   繁体   English

uidatepicker问题

[英]uidatepicker problem

I develop ipad application, I have a button, I want when click button a subview appear containing datepicker, done cancel我开发 ipad 应用程序,我有一个按钮,我想在单击按钮时出现一个包含 datepicker 的子视图,完成取消

the problem is that问题是

  1. the datepicked doesn't appear选择的日期没有出现
  2. when click cancel the application crash unrecognized selector sent to instance当单击取消应用程序崩溃无法识别的选择器发送到实例

I use the following code我使用以下代码

-(IBAction)BirthDatePicker:(id)sender{
    if(! ISPicker) {

        if( self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,700, 768, 216)];
            mytab = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 656, 768, 44)];


        }
        else {
            pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,439, 1024, 216)];
            mytab = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 395, 1024, 44)];


        }


        pickerView.alpha=0.0;
        mytab.alpha=0.0;

        //pickerView.showsSelectionIndicator = YES;
        [self.view addSubview:pickerView];
        [self.view bringSubviewToFront:pickerView]; 

        mytab.tintColor=[UIColor blackColor];

        UIBarButtonItem * bt1=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(_Done)];
        UIBarButtonItem * bt2=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(_cancel)];
        UIBarButtonItem * flx=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

        NSArray *arr=[[NSArray alloc] initWithObjects:flx,bt1,bt2,nil];
        [mytab setItems:arr];
        [self.view addSubview:mytab];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        pickerView.alpha=1.0;
        mytab.alpha=1.0;
        [UIView commitAnimations];

        [pickerView release];
        [mytab release];
        [bt1 release];
        [flx release];
        [arr release];

        ISPicker = true ;
    }
}

-(void)_Done{

    ISPicker  = false ;

    NSDate * selected = [pickerView date];
    NSString * date = [selected description];
    DateLabel.text = date;

}

-(void)_Cancel{ 

    ISPicker  = false ;
    //// Release the view 
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    pickerView.alpha=0.0;
    mytab.alpha=0.0;

    [UIView commitAnimations];
}

any suggestion please请有任何建议

You should not releasse your pickerView and myTab until you are done with it, remove the release from - (IBAction)BirthDatePicker:(id)sender and move to the -(void)_Done and -(void)_Cancel在完成之前,您不应释放您的pickerViewmyTab ,从 - (IBAction)BirthDatePicker:(id)sender中删除释放并移至-(void)_Done-(void)_Cancel

the wrong is in this line错误在这一行

pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,700, 768, 216)];

it should be它应该是

pickerView = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,700, 768, 216)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM