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)];

 

转载于:https://www.cnblogs.com/yzm157/archive/2013/01/18/2866320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值