开发者

Problem in Datepicker and alertsheet

开发者 https://www.devze.com 2023-02-07 10:51 出处:网络
I am implementing UIDATEPICKER. The problem is that when i click on the button, The alert sheet is appear and datepicker is add as subview but i want two button,

I am implementing UIDATEPICKER. The problem is that when i click on the button, The alert sheet is appear and datepicker is add as subview but i want two button,

1-cancel 2-done

what will i do?

UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:@"How many?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitle开发者_StackOverflows:nil];

aac.actionSheetStyle = UIActionSheetStyleBlackOpaque;

sheet = aac;
UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
if(IsDateSelected==YES)
{
    theDatePicker.datePickerMode = UIDatePickerModeDate;
    theDatePicker.maximumDate=[NSDate date];
}else {
    theDatePicker.datePickerMode = UIDatePickerModeTime;
}

self.dtpicker = theDatePicker;
[theDatePicker release];
[dtpicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];

pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickerCancelClick)];
[barItems addObject:cancelBtn];

//// it doesnot work ..I want this....

Problem in Datepicker and alertsheet

what will i do?


-(IBAction)DatePickerView
{
   iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

 pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"How many?"
                                                delegate:self
                                       cancelButtonTitle:nil
                                  destructiveButtonTitle:nil
                                       otherButtonTitles:nil];

   theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
   theDatePicker.datePickerMode = UIDatePickerModeDateAndTime;

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
   [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
     [dateFormatter setDateFormat:@"MM/dd/YY h:mm a"];
   [theDatePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];


   pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
   pickerToolbar.barStyle=UIBarStyleBlackOpaque;
   [pickerToolbar sizeToFit];   



   NSMutableArray *barItems = [[NSMutableArray alloc] init];   
   UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
   [barItems addObject:flexSpace];


   UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
                              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                              target:nil
                              action:nil];
   [barItems addObject:spacer];




   UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickerCancelClick)];
   [barItems addObject:cancelBtn];


   [pickerToolbar setItems:barItems animated:YES];       
   [pickerViewDate addSubview:pickerToolbar];
   [pickerViewDate addSubview:theDatePicker];
   [pickerViewDate  showInView:self.view];
   [pickerViewDate setBounds:CGRectMake(0,0,320, 464)];
   NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
   [currentdateformate setDateFormat:@"HH:MM, EEEE, dd-MMMM-yyyy"];
   appDelegate.timestamp1 = [currentdateformate stringFromDate:[theDatePicker date]];


   NSDateFormatter *currentdateformate1 = [[NSDateFormatter alloc] init];
   [currentdateformate1 setDateFormat:@"MMM dd, yyyy HH:mm"];
   self.updatedate = [currentdateformate1 stringFromDate:[theDatePicker date]];
}

-(IBAction)dateChanged{
   iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

   NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
   [currentdateformate setDateFormat:@"HH:MM, EEEE, dd-MMMM-yyyy"];

   appDelegate.timestamp1 = [currentdateformate stringFromDate:[theDatePicker date]];
   NSLog(@"%@",appDelegate.timestamp1);
   [currentdateformate setDateFormat:@"MMM dd, yyyy HH:mm"];
   self.updatedate = [currentdateformate stringFromDate:[theDatePicker date]];

}
-(void)DatePickerCancelClick
{
   self.pickerViewDate.hidden = YES;
   self.view.hidden = NO;
   [self.pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];

}

-(BOOL)closeDatePicker:(id)sender{   
   //iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];

   [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
   [pickerToolbar release];
   [pickerViewDate release];
   //[SelectedTextField resignFirstResponder];   

   if([ self.updatedate isEqualToString:@"nil"]){
       NSDateFormatter *currentdateformate = [[NSDateFormatter alloc] init];
       [currentdateformate setDateFormat:@"MMM dd, yyyy HH:mm"];
       self.updatedate = [currentdateformate stringFromDate:[theDatePicker date]];
        [dateSelectButton setTitle:self.updatedate forState:UIControlStateNormal];
        self.updatedate = @"";
   }
   else{
       [dateSelectButton setTitle:self.updatedate forState:UIControlStateNormal];
        self.updatedate = @"";
   }

   return YES;
}

-(IBAction)DatePickerDoneClick{   
   [self closeDatePicker:self];


}


maybe you should get rid of the UIActionSheet and do it like I did for another question.

UIDatePicker pop up after UIButton is pressed

0

精彩评论

暂无评论...
验证码 换一张
取 消