我知道新的UIAlertController,但我还没有看到除了创建UIViewController之外的其他解决方案,将UIPickerView添加为子视图并以模态方式呈现...这可能意味着添加代理来传递信息:(
任何人都可以发布一个使用带有选择器的UIAlertController的例子..所有在同一个控制器中?
由于
答案 0 :(得分:1)
对于iOS8 ActionSheetPicker-3.0是日期选择器和其他选择器的最佳解决方案。
答案 1 :(得分:1)
不要将DatePicker嵌入到UIActionSheet中!从iOS 7开始,UIActionSheet不应该包含任何自定义子视图,我可以向您保证,在iOS 8中,添加到ActionSheet的任何子视图都不显示!
你可以做什么代替并模拟一个ActionSheet如下(下面是一个日期选择器的例子,它从底部开始动画,并且还有一个UIToolbar,上面有按钮):
创建两个属性:
@property (strong, nonatomic) UIDatePicker *theDatePicker;
@property (strong, nonatomic) UIView *pickerView;
创建您的选择器,将其嵌入UIView并显示:
-(void) createDatePickerAndShow {
UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];
[controlToolbar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil] animated:NO];
[theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, theDatePicker.frame.size.width, theDatePicker.frame.size.height)];
if (!pickerView) {
pickerView = [[UIView alloc] initWithFrame:theDatePicker.frame];
} else {
[pickerView setHidden:NO];
}
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
CGFloat pickerViewYposition = self.view.frame.size.height - pickerView.frame.size.height;
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
[pickerView setBackgroundColor:[UIColor whiteColor]];
[pickerView addSubview:controlToolbar];
[pickerView addSubview:theDatePicker];
[theDatePicker setHidden:NO];
[self.view addSubview:pickerView];
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYposition,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
并解雇DatePicker:
-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + pickerView.frame.size.height;
[UIView animateWithDuration:0.5f
animations:^{
[pickerView setFrame:CGRectMake(pickerView.frame.origin.x,
pickerViewYpositionHidden,
pickerView.frame.size.width,
pickerView.frame.size.height)];
}
completion:nil];
}
答案 2 :(得分:1)
使用UIAlertController,UIActionSheet的首选替代品。然后使用addSubView将UIDatePicker添加到UIAlertController.view而不是UIActionSheet。设置UIAlertController的标题与UIActionSheet的标题相同。这是一个代码示例:
@interface MyControllerClass() {
@private UIDatePicker* datePicker;
}
@end
@implementation MyControllerClass
...
-(void) showDatePicker {
if (!datePicker)
datePicker = [[UIDatePicker alloc] init];
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1){
//Pre iOS 8
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"OK", nil];
[actionSheet addSubview:datePicker];
[actionSheet setTag:ACTION_SHEET_DATEPICKER];
[actionSheet showInView:self.aView];
} else {
//for iOS 8
UIAlertController* datePickerContainer = [UIAlertController alertControllerWithTitle: title
message:nil
preferredStyle: UIAlertControllerStyleActionSheet];
[datePickerContainer.view addSubview:datePicker];
//Add autolayout constraints to position the datepicker
[datePicker setTranslatesAutoresizingMaskIntoConstraints:NO];
// Create a dictionary to represent the view being positioned
NSDictionary *labelViewDictionary = NSDictionaryOfVariableBindings(datePicker);
NSArray* hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[datePicker]-|" options:0 metrics:nil views:labelViewDictionary];
[datePickerContainer.view addConstraints:hConstraints];
NSArray* vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[datePicker]" options:0 metrics:nil views:labelViewDictionary];
[datePickerContainer.view addConstraints:vConstraints];
[datePickerContainer addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[self dateSelected:nil];
}]];
[self presentViewController:datePickerContainer animated:YES completion:nil];
}
}
}
...
@end
请根据需要修改和改进。
答案 3 :(得分:0)
我认为你必须注意的第一条评论中有一点错误, 这是编辑后的代码:
@property (strong, nonatomic) UIDatePicker *theDatePicker;
@property (strong, nonatomic) UIView *pickerView;
- (IBAction)createDatePickerAndShow:(id)sender;
然后链接到createDatePickerAndShow按钮。 之后创建你的选择器,将其嵌入到UIView中并显示:
//.................Edit to (IBAction)createDatePickerAndShow:(id)sender
- (IBAction)createDatePickerAndShow:(id)sender {
UIToolbar *controlToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, _pickerView.bounds.size.width, 44)];
[controlToolbar sizeToFit];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
//............add this to disable the warning of @selector(dismissDateSet)]
#pragma GCC diagnostic ignored "-Wundeclared-selector"
UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];
//...........Edit to UIBarButtonItemStylePlain that is supported in ios 8
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelDateSet)];
[controlToolbar setItems:[NSArray arrayWithObjects: cancelButton, spacer, setButton, nil] animated:NO];
[_theDatePicker setFrame:CGRectMake(0, controlToolbar.frame.size.height - 15, _theDatePicker.frame.size.width, _theDatePicker.frame.size.height)];
if (!_pickerView) {
_pickerView = [[UIView alloc] initWithFrame:_theDatePicker.frame];
} else {
//.................here edit _pickerView
[_pickerView setHidden:NO];
}
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + _pickerView.frame.size.height;
CGFloat pickerViewYposition = self.view.frame.size.height - _pickerView.frame.size.height;
[_pickerView setFrame:CGRectMake(_pickerView.frame.origin.x,
pickerViewYpositionHidden,
_pickerView.frame.size.width,
_pickerView.frame.size.height)];
[_pickerView setBackgroundColor:[UIColor whiteColor]];
[_pickerView addSubview:controlToolbar];
[_pickerView addSubview:_theDatePicker];
[_theDatePicker setHidden:NO];
[self.view addSubview:_pickerView];
[UIView animateWithDuration:0.5f
animations:^{
[_pickerView setFrame:CGRectMake(_pickerView.frame.origin.x,
pickerViewYposition,
_pickerView.frame.size.width,
_pickerView.frame.size.height)];
}
completion:nil];
}
并解雇DatePicker:
-(void) cancelDateSet {
CGFloat pickerViewYpositionHidden = self.view.frame.size.height + _pickerView.frame.size.height;
[UIView animateWithDuration:0.5f
animations:^{
[_pickerView setFrame:CGRectMake(_pickerView.frame.origin.x,
pickerViewYpositionHidden,
_pickerView.frame.size.width,
_pickerView.frame.size.height)];
}
completion:nil];
}