UIActionSheet添加UIDatePicker

//在iphone中没有点击弹出选择时间的控件,下面就利用ios的UIActionSheet + UIDatePicker + UIToolBar 来实现弹出时间选择控件。代码如下:

1
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil]; 2 3 UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0f, 44.0f, 0.0f, 0.0f)]; 4 [datePicker setDate:[NSDate date]]; //显示当前时间 5 [datePicker setUserInteractionEnabled:YES]; // 6 datePicker.datePickerMode = UIDatePickerModeTime; //模式:显示时间 7 8 UIToolbar *pickerDateToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44)]; //创建工具条,用来设置或者退出actionsheet. 9 pickerDateToolBar.barStyle = UIBarStyleBlackOpaque; 10 [pickerDateToolBar sizeToFit]; 11 12 NSMutableArray *barItems = [[NSMutableArray alloc] init]; 13 14 UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 15 [barItems addObject:flexSpace]; 16 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil) style:UIBarButtonSystemItemCancel target:self action:@selector(datePickerCancelClick:)]; 17 [barItems addObject:cancelButton]; 18 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"OK", nil) style:UIBarButtonItemStyleDone target:self action:@selector(datePickerDoneClick:)]; 19 // UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(datePickerDoneClick:)]; 20 [barItems addObject:doneButton]; 21 22 [pickerDateToolBar setItems:barItems animated:YES]; //将按键加入toolbar 23 24 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 11.0f, 100.0f, 22.0f)]; 25 label.text = NSLocalizedString(@"SetTimeLabel", nil); 26 label.backgroundColor = [UIColor clearColor]; 27 label.textColor = [UIColor whiteColor]; 28 [label sizeToFit]; 29 [pickerDateToolBar addSubview:label]; 30 31 [actionSheet addSubview:pickerDateToolBar]; 32 [actionSheet addSubview:datePicker]; 33 // [actionSheet setBounds:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 260)]; 34 [actionSheet showInView: [UIApplication sharedApplication].keyWindow]; //这里使用全局的键盘的view,可以避免在有tabBar或者toolBar的页面,把actionSheet下方挡住。 35 // [actionSheet setBounds:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 260)];

 

posted on 2013-01-18 14:09  yzm157  阅读(701)  评论(0编辑  收藏  举报