在编码过程中,我们经常遇到需要这样一个效果,就是弹出框的嵌套;
举个最简单的例子,比如你要选择时间,必然需要一个时间选择器DatePicker.但是这个选择器又是在你点击某按钮时弹出,弹出方式最常见的就是上拉菜单了,所以这就涉及了AlertController嵌入DatePicker; 因为前一篇我已经说过了,在IOS8之后,苹果已经废弃了actionSheet;那么我们就要重新来玩这个嵌套了.
那么我拿刚才写的一个小demo来说一下
#define BUTTONSIZE 40
// datePicker创建方法 - (void)creatDatePicker { self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0*WIDTH, 20*HEIGHT, 320*WIDTH, 216)]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; // 设置中文显示 self.datePicker.locale = locale; // 设置时区 [self.datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+0800"]]; // 设置当前显示时间 // 设置初始时间 [self.datePicker setDate:[NSDate date] animated:YES]; // 设置选择器类型 self.datePicker.datePickerMode = UIDatePickerModeDate; [self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; }
- (void)datePickerValueChanged:(UIDatePicker*)datePicker { }
<pre name="code" class="objc">// 弹出上拉菜单 - (void)actionSheetShow { self.alertController = [UIAlertController alertControllerWithTitle:@" " message:@" " preferredStyle:UIAlertControllerStyleActionSheet]; // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, 360, 316)]; // imageView.image = [UIImage imageNamed:@"SlotMachineBackground@2x.png"]; // [alertController.view addSubview:imageView]; UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(-10, -20, 385, 300)]; imageView2.image = [UIImage imageNamed:@"pickerBack.png"]; UIButton *sureButton = [self creatButtonWithFrame:CGRectMake(340, 5, 0.75 * BUTTONSIZE, 0.75 * BUTTONSIZE) action:@selector(dismissAlert:) image:[UIImage imageNamed:@"FBDsureImage"] title:nil]; sureButton.tag = 1000; UIButton *errorButton = [self creatButtonWithFrame:CGRectMake(5, 5, 0.75 * BUTTONSIZE, 0.75 * BUTTONSIZE) action:@selector(dismissAlert:) image:[UIImage imageNamed:@"FBDerrorImage"] title:nil]; errorButton.tag = 1001; [imageView2 addSubview:sureButton]; [imageView2 addSubview:errorButton]; imageView2.userInteractionEnabled = YES; [self.alertController.view addSubview:imageView2]; [self.alertController.view addSubview:self.datePicker]; [self presentViewController:self.alertController animated:YES completion:nil]; }
// 创建button - (UIButton *)creatButtonWithFrame:(CGRect)frame action:(SEL)sel image:(UIImage *)image title:(NSString *)title { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = frame; [button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside]; [button setImage:image forState:UIControlStateNormal]; [button setTitle:title forState:UIControlStateNormal]; button.showsTouchWhenHighlighted = YES; return button; }
这样就实现了一个简单的AlertController嵌入DatePicker