Search code examples
iphonexcode4datepicker

How to display DatePicker in popupwindow in iPhone?


I m newbie to objective-C

I want to display the UIDatePicker in a popup window after the button click ..

I have a button and when I click the button my popup should appear with DatePicker and later after chosing the date the popup should close and set the selected date in a textbox.

How can I do this ?

To create a datepicker I wrote this code ..

UIDatePicker *datePicker=[[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];

But I do not know how to display it in a popup window on a button click ..?


Solution

  • Declaration in your .h file

    UIActionSheet *aac;  
    UIDatePicker *theDatePicker; 
    

    Implementation in .m file

    // Add the code after your comment

    -(void)DatePickerDoneClick:(id)sender {
    
            NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
            df.dateFormat = @"MM/dd/yyyy";
            NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:theDatePicker.date]] componentsSeparatedByString:@""];
    
            [dateString1 release];
            dateString1=nil;
            dateString1 = [[NSString alloc]initWithString:[temp objectAtIndex:0]];
        UITextField* BirthDayTxtLBl.text = [NSString stringWithFormat:@" %@",dateString1];
    
    NSString *theTime = [NSString stringWithFormat:@"%@",BirthDayTxtLBl.text];
            NSLog(@"%@",theTime);
            [aac dismissWithClickedButtonIndex:0 animated:YES];
    
    
    
            }     
    
    
    
    
    
    - (void)DatePickercancelClick:(id)sender{
    
        [aac dismissWithClickedButtonIndex:0 animated:YES];
    }
    
    -(IBAction)AddTheTimePicker:(id)sendar {
    
        aac = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?@"\n\n":nil  delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
        theDatePicker.datePickerMode=UIDatePickerModeDateAndTime;
        UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 320, 44)];
        pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerDateToolbar sizeToFit];
    
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick:)];
        //doneBtn.tag = tagID;
        [barItems addObject:doneBtn];
    
    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
        UILabel *toolBarItemlabel;
        if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
            toolBarItemlabel= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
        else
            toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200,30)];
    
        [toolBarItemlabel setTextAlignment:UITextAlignmentCenter];  
        [toolBarItemlabel setTextColor:[UIColor whiteColor]];   
        [toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];    
        [toolBarItemlabel setBackgroundColor:[UIColor clearColor]]; 
        toolBarItemlabel.text = [NSString stringWithFormat:@"Select Start Time"];
    
        UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
        [toolBarItemlabel release]; 
        [barItems addObject:buttonLabel];   
        [buttonLabel release];  
    
        [barItems addObject:flexSpace];
    
        UIBarButtonItem *SelectBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickercancelClick:)];
        [barItems addObject:SelectBtn];
    
        [pickerDateToolbar setItems:barItems animated:YES];
        [aac addSubview:pickerDateToolbar];
        [aac addSubview:theDatePicker];
    
        CGRect myImageRect = CGRectMake(0.0f, 300.0f, 320.0f, 175.0f);;
        [aac showFromRect:myImageRect inView:self.view animated:YES ];
    
        [UIView beginAnimations:nil context:nil];
        if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
            [aac setBounds:CGRectMake(0,0,320, 464)];
        else
            [aac setBounds:CGRectMake(0,0,480, 400)];       
    
        [UIView commitAnimations];
    
    }
    
    // Add this if you wish to add support Orientation support for picker
      - (BOOL) isViewPortrait {
         UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
         return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }