Sealyu 2010-12-22

这两天在做一个日期选择的,不想换页显示,就想使用UIActionSheet在本页弹出选择框,代码如下:
#pragma mark DatePickerView
UIImagePickerController* imagePickerController;
UIDatePicker *theDatePicker;
UIToolbar* pickerToolbar;
UIActionSheet* pickerViewDate;
-(void)DatePickerView
{
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 = UIDatePickerModeDate;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@”en_US”]autorelease]];
[dateFormatter setDateFormat:@”dd MMM yyyy”];
//[dateFormatter setDateFormat:@”MM/dd/YYYY”];

//[theDatePicker release];
[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];

[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:theDatePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)];
}

-(IBAction)dateChanged{

NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[[NSLocale alloc]
initWithLocaleIdentifier:@”en_US”] autorelease]];
[FormatDate setDateFormat:@”MM/dd/YYYY”];
SelectedTextField.text = [FormatDate stringFromDate:[theDatePicker date]];
}

-(BOOL)closeDatePicker:(id)sender{
[pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
[pickerToolbar release];
[pickerViewDate release];
[SelectedTextField resignFirstResponder];

return YES;
}

-(IBAction)DatePickerDoneClick{
[self closeDatePicker:self];
tableview.frame=CGRectMake(0, 44, 320, 416);

}

– (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}

// returns the number of rows
– (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 30;
// return [pickerViewArray count];
}

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注