尝试将UIDatePickerView
显示为dialog box
,如Android
所示。我尝试将其添加到UIAlertView
但无法添加。
有人试过吗?
答案 0 :(得分:0)
iOS7中的
中无法使用AddSubviewUIAlertView
UIAlertView
类旨在按原样使用,不支持子类化。
此类的视图层次结构是私有的,不得修改。
您应该使用UIAlertViewcontoller
。这是代码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"OK");
NSLog(@"%@",picker.date);
}];
action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController animated:YES completion:nil];
希望这会对你有帮助....
答案 1 :(得分:0)
在按钮操作中,我设置了datePicker
ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
UIDatePicker *datepicker;
}
@end
@implementation ViewController
-(IBAction) showDatePicker:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert];
datepicker = [[UIDatePicker alloc]init];
[datepicker setDatePickerMode:UIDatePickerModeDateAndTime];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:@selector(getPickerValue:) forControlEvents:UIControlEventValueChanged];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The selected or picked datePicker value is - %@",datepicker.date);
txtfldGetDatePickerValue.text = [self getStringDateFromDate:datepicker.date];
}];
action;
})];
[alertController.view addSubview:datepicker];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)getPickerValue:(UIDatePicker *)pickerDate
{
txtfldGetDatePickerValue.text = [self getStringDateFromDate:pickerDate.date];
}
-(NSString *)getStringDateFromDate:(NSDate *)pickerDate
{
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
return [formatter stringFromDate:pickerDate];
}
我尝试了很多解决方案,最后我得到了输出。由于我没有正确给出答案,我想给你的问题给出最好的答案。我搜索了很多答案。即使我创建了示例项目来实现这一点。我没有使用动作表和popoverpresentration视图controller.Simply我使用了UIAlertViewController编码。
UIDatePicker to action sheet was discouraged by Apple all along
答案 2 :(得分:0)
这将有效..查看代码。这将适用于ios7及以上
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Date" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)];
[alert addSubview:picker];
alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);
[alert setValue:picker forKey:@"accessoryView"];
[alert show];
答案 3 :(得分:0)
用于显示手机中的弹出窗口只需制作UIPopoverController的类别 代码:
// UIPopoverController+iPhone.h
#import <UIKit/UIKit.h>
@interface UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled;
@end
// UIPopoverController+iPhone.m
#import "UIPopoverController+iPhone.h"
@implementation UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled {
return NO;
}
@end
这肯定会帮助你。